Skip to content

Commit

Permalink
fix: initialize logger immediately instead of using the autofac module
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
alsami committed Oct 7, 2019
1 parent a223073 commit 9bca8ce
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ public static ContainerBuilder RegisterSerilog(this ContainerBuilder builder, st
LogEventLevel logEventLevel = LogEventLevel.Debug, string outputTemplate = Constants.DefaultLogTemplate)
{
if (string.IsNullOrWhiteSpace(logPath))
{
throw new ArgumentNullException(nameof(logPath));
}

if (string.IsNullOrWhiteSpace(outputTemplate))
{
throw new ArgumentNullException(nameof(outputTemplate));
}

return RegisterSerilogInternal(builder,
Constants.DefaultLoggerConfiguration(logPath, outputTemplate, logEventLevel));
Expand All @@ -30,11 +26,12 @@ private static ContainerBuilder RegisterSerilogInternal(this ContainerBuilder bu
LoggerConfiguration loggerConfiguration)
{
if (loggerConfiguration == null)
{
throw new ArgumentNullException(nameof(loggerConfiguration));
}

builder.RegisterModule(new SerilogModule(loggerConfiguration));
Log.Logger = loggerConfiguration
.CreateLogger();

builder.RegisterModule<SerilogModule>();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,8 @@ namespace Serilog.Extensions.Autofac.DependencyInjection
{
internal class SerilogModule : Module
{
private readonly LoggerConfiguration loggerConfiguration;

public SerilogModule(LoggerConfiguration loggerConfiguration)
{
this.loggerConfiguration = loggerConfiguration;
}

protected override void Load(ContainerBuilder builder)
{
Log.Logger = this.loggerConfiguration
.CreateLogger();

builder.Register(_ => new LoggerFactory(new ILoggerProvider[] {new SerilogLoggerProvider()}))
.As<ILoggerFactory>()
.SingleInstance();
Expand Down

0 comments on commit 9bca8ce

Please sign in to comment.