diff --git a/README.md b/README.md index 5f06ff4..6ac3b6f 100644 --- a/README.md +++ b/README.md @@ -88,3 +88,42 @@ Tip: to see Serilog output in the Visual Studio output window when running under With _Serilog.AspNetCore_ installed and configured, you can write log messages directly through Serilog or any `ILogger` interface injected by ASP.NET. All loggers will use the same underlying implementation, levels, and destinations. **Tip:** change the minimum level for `Microsoft` to `Warning` and plug in this [custom logging middleware](https://github.com/datalust/serilog-middleware-example/blob/master/src/Datalust.SerilogMiddlewareExample/Diagnostics/SerilogMiddleware.cs) to clean up request logging output and record more context around errors and exceptions. + +### Alternative configuration + +You can chose to build the logger as part of the `WebHostBuilder` pipeline, and thus benefit from the application configuration. +The following code shows an example of such a configuration: + +````csharp +public class Program +{ + public static void Main(string[] args) + { + var host = new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + // Load the application configuration over the web host configuration. + .ConfigureAppConfiguration((hostingContext, configurationBuilder) => + { + configurationBuilder + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + }) + // Configure Serilog to be used as the logger for the whole application. + .UseSerilog((hostingContext, loggerConfiguration) => + loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration) + .Enrich.FromLogContext() + .WriteTo.Console() + ) + .UseIISIntegration() + .UseStartup() + .Build(); + + host.Run(); + } +} +```` + +With this code, the default behavior is to set the created `ILogger` as the default logger. `Log.Logger` can be used as usual to access the created logger. diff --git a/serilog-aspnetcore.sln b/serilog-aspnetcore.sln index 3957472..e7f7ca0 100644 --- a/serilog-aspnetcore.sln +++ b/serilog-aspnetcore.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.10 +VisualStudioVersion = 15.0.26730.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}" EndProject diff --git a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs index c7a1df1..f15d9c2 100644 --- a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs +++ b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs @@ -38,7 +38,40 @@ public static IWebHostBuilder UseSerilog(this IWebHostBuilder builder, Serilog.I { if (builder == null) throw new ArgumentNullException(nameof(builder)); builder.ConfigureServices(collection => - collection.AddSingleton(new SerilogLoggerFactory(logger, dispose))); + collection.AddSingleton(services => new SerilogLoggerFactory(logger, dispose))); + return builder; + } + + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The web host builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// The web host builder. + public static IWebHostBuilder UseSerilog(this IWebHostBuilder builder, Action configureLogger, bool preserveStaticLogger = false) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); + builder.ConfigureServices((context, collection) => + { + var loggerConfiguration = new LoggerConfiguration(); + configureLogger(context, loggerConfiguration); + var logger = loggerConfiguration.CreateLogger(); + if (preserveStaticLogger) + { + collection.AddSingleton(services => new SerilogLoggerFactory(logger, true)); + } + else + { + // 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 => new SerilogLoggerFactory(null, true)); + } + }); return builder; } }