forked from gigya/microdot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNLogModule.cs
More file actions
53 lines (49 loc) · 1.83 KB
/
Copy pathNLogModule.cs
File metadata and controls
53 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using Gigya.Microdot.Interfaces.Events;
using Gigya.Microdot.Interfaces.Logging;
using Gigya.Microdot.Ninject;
using Ninject.Activation;
using Ninject.Modules;
using Ninject.Syntax;
namespace Gigya.Microdot.Logging.NLog
{
/// <summary>
/// Configures the logger to be <see cref="NLogLogger"/> (instance per type) and the event publisher to be
/// <see cref="NullEventPublisher"/>.
/// </summary>
public class NLogModule : NinjectModule, ILoggingModule
{
/// <summary>
/// Used by clients to initialize logging and tracing.
/// </summary>
public override void Load()
{
Bind(Bind<ILog>(), Bind<IEventPublisher>());
}
/// <summary>
/// Used by Microdot hosts to initialize logging and tracing.
/// </summary>
/// <param name="logBinding"></param>
/// <param name="eventPublisherBinding"></param>
public void Bind(IBindingToSyntax<ILog> logBinding, IBindingToSyntax<IEventPublisher> eventPublisherBinding)
{
logBinding
.To<NLogLogger>()
.InScope(GetTypeOfTarget)
.WithConstructorArgument("receivingType", (context, target) => GetTypeOfTarget(context));
eventPublisherBinding
.To<LogEventPublisher>()
.InSingletonScope();
}
/// <summary>
/// Returns the type that requested the log, or the type <see cref="NLogModule"/> if the requester can't be determined.
/// </summary>
/// <param name="context">The Ninject context of the request.</param>
/// <returns></returns>
private static Type GetTypeOfTarget(IContext context)
{
var type = context.Request.Target?.Member.DeclaringType;
return type ?? typeof(NLogModule);
}
}
}