Created
November 15, 2017 14:50
-
-
Save jonnii/5d9340c38d157df837d20ef881511c3d to your computer and use it in GitHub Desktop.
logging facility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingFacility : AbstractFacility | |
{ | |
private ILogger logger; | |
protected override void Init() | |
{ | |
if (logger == null) | |
{ | |
return; | |
} | |
Kernel.Resolver.AddSubResolver(new SerilogSubResolver(logger)); | |
} | |
public void UseSerilog(Func<LoggerConfiguration> createLoggingConfiguration) | |
{ | |
if (logger != null) | |
{ | |
throw new InvalidOperationException("You cannot call UseSerilog() twice."); | |
} | |
logger = createLoggingConfiguration().CreateLogger(); | |
} | |
public class SerilogSubResolver : ISubDependencyResolver | |
{ | |
private readonly ILogger buildLogger; | |
public SerilogSubResolver(ILogger buildLogger) | |
{ | |
this.buildLogger = buildLogger; | |
} | |
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) | |
{ | |
return dependency.TargetType == typeof(ILogger); | |
} | |
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) | |
{ | |
return buildLogger.ForContext(model.Implementation); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment