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
**Next**, in your application's _Program.cs_ file, configure Serilog first:
15
+
**Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged:
16
16
17
17
```csharp
18
18
publicclassProgram
@@ -25,25 +25,11 @@ public class Program
25
25
.Enrich.FromLogContext()
26
26
.WriteTo.Console()
27
27
.CreateLogger();
28
-
```
29
-
30
-
Then, add `UseSerilog()` tothewebhostbuilder. A `try`/`catch` blockwillensureanyconfigurationissuesareappropriatelylogged:
31
28
32
-
```csharp
33
29
try
34
30
{
35
31
Log.Information("Starting web host");
36
-
37
-
varhost=newWebHostBuilder()
38
-
.UseKestrel()
39
-
.UseContentRoot(Directory.GetCurrentDirectory())
40
-
.UseIISIntegration()
41
-
.UseStartup<Startup>()
42
-
.UseSerilog() // <-- Add this line
43
-
.Build();
44
-
45
-
host.Run();
46
-
32
+
BuildWebHost(args).Run();
47
33
return0;
48
34
}
49
35
catch (Exceptionex)
@@ -56,6 +42,16 @@ Then, add `UseSerilog()` to the web host builder. A `try`/`catch` block will ens
@@ -83,6 +79,25 @@ That's it! With the level bumped up a little you will see log output like:
83
79
84
80
Tip: to see Serilog output in the Visual Studio output window when running under IIS, select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list.
85
81
82
+
A more complete example, showing _appsettings.json_ configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/SimpleWebSample).
83
+
86
84
### Using the package
87
85
88
86
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.
87
+
88
+
**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.
89
+
90
+
### Inline initialization
91
+
92
+
You can alternatively configure Serilog using a delegate as shown below:
This has the advantage of making the `hostingContext`'s `Configuration` object available for configuration of the logger, but at the expense of recording `Exception`s raised earlier in program startup.
102
+
103
+
If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down.
/// <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