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
Serilog logging for ASP.NET Core. This package routes ASP.NET Core log messages through Serilog, so you can get information about ASP.NET's internal operations written to the same Serilog sinks as your application events.
4
4
5
5
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.
6
6
7
+
**.NET Framework** and .NET Core 2.x are supported by version 3.4.0 of this package. Recent versions of _Serilog.AspNetCore_ require .NET Core 3.x, .NET 5, or later.
8
+
7
9
### Instructions
8
10
9
11
**First**, install the _Serilog.AspNetCore_[NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app.
**Finally**, clean up by removing the remaining configuration for the default logger:
63
64
64
-
* Remove the `"Logging"` section from _appsettings.json_ files (this can be replaced with [Serilog configuration](https://github.com/serilog/serilog-settings-configuration) as shown in [the _EarlyInitializationSample_ project](https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/EarlyInitializationSample/Program.cs), if required)
65
+
* Remove the `"Logging"` section from _appsettings.*.json_ files (this can be replaced with [Serilog configuration](https://github.com/serilog/serilog-settings-configuration) as shown in [the _Sample_ project](https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/Sample/Program.cs), if required)
65
66
* Remove `UseApplicationInsights()` (this can be replaced with the [Serilog AI sink](https://github.com/serilog/serilog-sinks-applicationinsights), if required)
66
67
67
68
That's it! With the level bumped up a little you will see log output resembling:
@@ -81,7 +82,7 @@ That's it! With the level bumped up a little you will see log output resembling:
81
82
82
83
**Tip:** to see Serilog output in the Visual Studio output window when running under IIS, either select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list, or replace `WriteTo.Console()` in the logger configuration with `WriteTo.Debug()`.
83
84
84
-
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/EarlyInitializationSample).
85
+
A more complete example, including`appsettings.json` configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/Sample).
85
86
86
87
### Request logging
87
88
@@ -146,7 +147,8 @@ During request processing, additional properties can be attached to the completi
ThedownsideofinitializingSerilogfirstisthatservicesfromtheASP.NETCorehost, includingthe `appsettings.json` configurationanddependencyinjection, aren't available yet.
200
197
201
-
Thishastheadvantageofmakingaserviceproviderandthe `hostingContext`'s `Configuration` object available for [configuration of the logger](https://github.com/serilog/serilog-settings-configuration), but at the expense of losing `Exception`s raised earlier in program startup.
198
+
Toaddressthis, Serilogsupportstwo-stageinitialization. Aninitial"bootstrap"loggerisconfiguredimmediately when the program starts, and this is replaced by the fully-configured logger once the host has loaded.
.CreateBootstrapLogger(); // <-- Change this line!
214
+
```
208
215
209
-
Serilogsendseventstooutputscalled_sinks_, thatimplementSerilog's `ILogEventSink` interface, and are added to the logging pipeline using `WriteTo`. _Microsoft.Extensions.Logging_ has a similar concept called _providers_, and these implement `ILoggerProvider`. Providers are what the default logging configuration creates under the hood through methods like `AddConsole()`.
Bydefault, Serilogignoresproviders, sincethereareusuallyequivalentSerilogsinksavailable, andtheseworkmoreefficientlywithSerilog's pipeline. If provider support is needed, it can be optionally enabled.
It's important to note that the final logger **completely replaces** the bootstrap logger: if you want both to log to the console, for instance, you'llneedtospecify `WriteTo.Console()` inbothplaces, astheexampleshows.
Serilogsendseventstooutputscalled_sinks_, thatimplementSerilog's `ILogEventSink` interface, and are added to the logging pipeline using `WriteTo`. _Microsoft.Extensions.Logging_ has a similar concept called _providers_, and these implement `ILoggerProvider`. Providers are what the default logging configuration creates under the hood through methods like `AddConsole()`.
235
251
236
-
**Usinginlineinitialization:**
252
+
Bydefault, Serilogignoresproviders, sincethereareusuallyequivalentSerilogsinksavailable, andtheseworkmoreefficientlywithSerilog's pipeline. If provider support is needed, it can be optionally enabled.
237
253
238
-
If [inlineinitialization](#inline-initialization) isused, providerscanbeenabledbyadding`writeToProviders: true` tothe `UseSerilog()`methodcall:
0 commit comments