You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39Lines changed: 39 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -88,3 +88,42 @@ Tip: to see Serilog output in the Visual Studio output window when running under
88
88
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.
89
89
90
90
**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.
91
+
92
+
### Alternative configuration
93
+
94
+
You can chose to build the logger as part of the `WebHostBuilder` pipeline, and thus benefit from the application configuration.
95
+
The following code shows an example of such a configuration:
96
+
97
+
````csharp
98
+
publicclassProgram
99
+
{
100
+
publicstaticvoidMain(string[] args)
101
+
{
102
+
varhost=newWebHostBuilder()
103
+
.UseKestrel()
104
+
.UseContentRoot(Directory.GetCurrentDirectory())
105
+
// Load the application configuration over the web host configuration.
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.
/// <summary>Sets Serilog as the logging provider.</summary>
46
+
/// <remarks>
47
+
/// A <see cref="WebHostBuilderContext"/> is supplied so that configuration and hosting information can be used.
48
+
/// The logger will be shut down when application services are disposed.
49
+
/// </remarks>
50
+
/// <param name="builder">The web host builder to configure.</param>
51
+
/// <param name="configureLogger">The delegate for configuring the <see cref="LoggerConfiguration" /> that will be used to construct a <see cref="Logger" />.</param>
52
+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Log.Logger"/>.</param>
0 commit comments