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
**Finally**, clean up by removing the remaining configuration for the default logger:
59
59
60
60
* Remove calls to `AddLogging()`
61
-
* 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 [this example](https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/SimpleWebSample/Program.cs), if required)
61
+
* 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)
62
62
* Remove `ILoggerFactory` parameters and any `Add*()` calls on the logger factory in _Startup.cs_
63
63
* Remove `UseApplicationInsights()` (this can be replaced with the [Serilog AI sink](https://github.com/serilog/serilog-sinks-applicationinsights), if required)
64
64
65
-
That's it! With the level bumped up a little you will see log output like:
65
+
That's it! With the level bumped up a little you will see log output resembling:
66
66
67
67
```
68
68
[22:14:44.646 DBG] RouteCollection.RouteAsync
@@ -79,7 +79,7 @@ That's it! With the level bumped up a little you will see log output like:
79
79
80
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.
81
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).
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/EarlyInitializationSample).
83
83
84
84
### Using the package
85
85
@@ -89,21 +89,60 @@ With _Serilog.AspNetCore_ installed and configured, you can write log messages d
89
89
90
90
### Inline initialization
91
91
92
-
You can alternatively configure Serilog using a delegate as shown below:
92
+
You can alternatively configure Serilog inline, in `BulidWebHost()`, 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.
103
103
104
104
If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down.
105
105
106
-
Note: Configuring Serilog via the `hostingContext`'s `Configuration` object requires that you've installed the _Serilog.Settings.Configuration_[NuGet package](https://www.nuget.org/packages/Serilog.Settings.Configuration).
106
+
A complete example, showing this approach, can be found in [the _InlineIntializationSample_ project](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/InlineInitializationSample).
Serilog sends events to outputs called _sinks_, that implement Serilog'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()`.
111
+
112
+
By default, Serilog ignores providers, since there are usually equivalent Serilog sinks available, and these work more efficiently with Serilog's pipeline. If provider support is needed, it can be optionally enabled.
113
+
114
+
**Using the recommended configuration:**
115
+
116
+
In the recommended configuration (in which startup exceptions are caught and logged), first create a `LoggerProviderCollection` in a static field in _Program.cs_:
Next, add `WriteTo.Providers()` to the logger configuration:
124
+
125
+
```csharp
126
+
.WriteTo.Providers(Providers)
127
+
```
128
+
129
+
Finally, pass the provider collection into `UseSerilog()`:
130
+
131
+
```csharp
132
+
.UseSerilog(providers: Providers)
133
+
```
134
+
135
+
Providers registered in _Startup.cs_ with `AddLogging()` will then receive events from Serilog.
136
+
137
+
**Using iniline initialization:**
138
+
139
+
If [inline initialization](#inline-initialization) is used, providers can be enabled by adding `writeToProviders: true` to the `UseSerilog()` method call:
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19
+
</p>
20
+
<p>
21
+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22
+
It can result in displaying sensitive information from exceptions to end users.
23
+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
0 commit comments