diff --git a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs index 8a2e673..6d22578 100644 --- a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs +++ b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs @@ -88,6 +88,7 @@ public static IWebHostBuilder UseSerilog( { if (builder == null) throw new ArgumentNullException(nameof(builder)); if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); + builder.ConfigureServices((context, collection) => { var loggerConfiguration = new LoggerConfiguration(); @@ -105,13 +106,13 @@ public static IWebHostBuilder UseSerilog( ILogger registeredLogger = null; if (preserveStaticLogger) { - // Passing a `null` logger to `SerilogLoggerFactory` results in disposal via - // `Log.CloseAndFlush()`, which additionally replaces the static logger with a no-op. - Log.Logger = logger; + registeredLogger = logger; } else { - registeredLogger = logger; + // Passing a `null` logger to `SerilogLoggerFactory` results in disposal via + // `Log.CloseAndFlush()`, which additionally replaces the static logger with a no-op. + Log.Logger = logger; } collection.AddSingleton(services =>