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.
3
4
4
-
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 logged to the same Serilog sinks as your application events.
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.
5
6
6
7
### Instructions
7
8
8
-
**First**, install the _Serilog.AspNetCore_[NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app. You will need a way to view the log messages - _Serilog.Sinks.Console_ writes these to the console; there are [many more sinks available](https://www.nuget.org/packages?q=Tags%3A%22serilog%22) on NuGet.
9
+
**First**, install the _Serilog.AspNetCore_[NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app.
**Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged:
**Finally**, clean up by removing the remaining configuration for the default logger:
59
60
60
61
* 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)
62
+
* 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
63
* Remove `ILoggerFactory` parameters and any `Add*()` calls on the logger factory in _Startup.cs_
63
64
* Remove `UseApplicationInsights()` (this can be replaced with the [Serilog AI sink](https://github.com/serilog/serilog-sinks-applicationinsights), if required)
64
65
65
-
That's it! With the level bumped up a little you will see log output like:
66
+
That's it! With the level bumped up a little you will see log output resembling:
66
67
67
68
```
68
69
[22:14:44.646 DBG] RouteCollection.RouteAsync
69
-
Routes:
70
-
Microsoft.AspNet.Mvc.Routing.AttributeRoute
71
-
{controller=Home}/{action=Index}/{id?}
72
-
Handled? True
70
+
Routes:
71
+
Microsoft.AspNet.Mvc.Routing.AttributeRoute
72
+
{controller=Home}/{action=Index}/{id?}
73
+
Handled? True
73
74
[22:14:44.647 DBG] RouterMiddleware.Invoke
74
-
Handled? True
75
+
Handled? True
75
76
[22:14:45.706 DBG] /lib/jquery/jquery.js not modified
76
77
[22:14:45.706 DBG] /css/site.css not modified
77
78
[22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css
78
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
+
**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()`.
81
82
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
+
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
84
84
-
### Using the package
85
+
### Request logging <sup>`3.0.0-*`</sup>
85
86
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
+
The package includes middleware for smarter HTTP request logging. The default request logging implemented by ASP.NET Core is noisy, with multiple events emitted per request. The included middleware condenses these into a single event that carries method, path, status code, and timing information.
88
+
89
+
As text, this has a format like:
90
+
91
+
```
92
+
[16:05:54 INF] HTTP GET / responded 200 in 227.3253 ms
93
+
```
94
+
95
+
Or [as JSON](https://github.com/serilog/serilog-formatting-compact):
96
+
97
+
```json
98
+
{
99
+
"@t": "2019-06-26T06:05:54.6881162Z",
100
+
"@mt": "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms",
101
+
"@r": ["224.5185"],
102
+
"RequestMethod": "GET",
103
+
"RequestPath": "/",
104
+
"StatusCode": 200,
105
+
"Elapsed": 224.5185,
106
+
"RequestId": "0HLNPVG1HI42T:00000001",
107
+
"CorrelationId": null,
108
+
"ConnectionId": "0HLNPVG1HI42T"
109
+
}
110
+
```
111
+
112
+
To enable the middleware, first change the minimum level for `Microsoft` to `Warning` in your logger configuration or _appsettings.json_ file:
app.UseSerilogRequestLogging(); // <-- Add this line
133
+
134
+
// Other app configuration
135
+
```
136
+
137
+
It's important that the `UseSerilogRequestLogging()` call appears _before_ handlers such as MVC. The middleware will not time or log components that appear before it in the pipeline. (This can be utilized to exclude noisy handlers from logging, such as `UseStaticFiles()`, by placing `UseSerilogRequestLogging()` after them.)
**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.
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.
173
+
Thishastheadvantageofmakingthe `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.
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()`.
182
+
183
+
Bydefault, Serilogignoresproviders, sincethereareusuallyequivalentSerilogsinksavailable, andtheseworkmoreefficientlywithSerilog's pipeline. If provider support is needed, it can be optionally enabled.
0 commit comments