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
+28-43
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,10 @@ Serilog logging for ASP.NET Core. This package routes ASP.NET Core log messages
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.
7
+
**Versioning:** This package tracks the versioning and target framework support of its
8
+
[_Microsoft.Extensions.Hosting_](https://nuget.org/packages/Microsoft.Extensions.Hosting) dependency. Most users should choose the version of _Serilog.AspNetCore_ that matches
9
+
their application's target framework. I.e. if you're targeting .NET 7.x, choose a 7.x version of _Serilog.AspNetCore_. If
10
+
you're targeting .NET 8.x, choose an 8.x _Serilog.AspNetCore_ version, and so on.
8
11
9
12
### Instructions
10
13
@@ -28,11 +31,9 @@ try
28
31
Log.Information("Starting web application");
29
32
30
33
varbuilder=WebApplication.CreateBuilder(args);
31
-
32
-
builder.Host.UseSerilog(); // <-- Add this line
34
+
builder.Services.AddSerilog(); // <-- Add this line
33
35
34
36
varapp=builder.Build();
35
-
36
37
app.MapGet("/", () =>"Hello World!");
37
38
38
39
app.Run();
@@ -47,23 +48,21 @@ finally
47
48
}
48
49
```
49
50
50
-
The `builder.Host.UseSerilog()` call will redirect all log events through your Serilog pipeline.
51
+
The `builder.Services.AddSerilog()` call will redirect all log events through your Serilog pipeline.
51
52
52
53
**Finally**, clean up by removing the remaining configuration for the default logger, including 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).
53
54
54
55
That's it! With the level bumped up a little you will see log output resembling:
55
56
56
57
```
57
-
[22:14:44.646 DBG] RouteCollection.RouteAsync
58
-
Routes:
59
-
Microsoft.AspNet.Mvc.Routing.AttributeRoute
60
-
{controller=Home}/{action=Index}/{id?}
61
-
Handled? True
62
-
[22:14:44.647 DBG] RouterMiddleware.Invoke
63
-
Handled? True
64
-
[22:14:45.706 DBG] /lib/jquery/jquery.js not modified
65
-
[22:14:45.706 DBG] /css/site.css not modified
66
-
[22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css
58
+
[12:01:43 INF] Starting web application
59
+
[12:01:44 INF] Now listening on: http://localhost:5000
60
+
[12:01:44 INF] Application started. Press Ctrl+C to shut down.
[12:01:47 WRN] Failed to determine the https port for redirect.
64
+
[12:01:47 INF] Hello, world!
65
+
[12:01:47 INF] HTTP GET / responded 200 in 95.0581 ms
67
66
```
68
67
69
68
**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()`.
@@ -97,12 +96,16 @@ Or [as JSON](https://github.com/serilog/serilog-formatting-compact):
97
96
}
98
97
```
99
98
100
-
To enable the middleware, first change the minimum level for `Microsoft.AspNetCore` to `Warning` in your logger configuration or _appsettings.json_ file:
99
+
To enable the middleware, first change the minimum level for the noisy ASP.NET Core log sources to `Warning` in your logger configuration or _appsettings.json_ file:
> **Tip:** add `{SourceContext}` to your console logger's output template to see the names of loggers; this can help track down the source of a noisy log event to suppress.
108
+
106
109
Then, in your application's _Program.cs_, add the middleware with `UseSerilogRequestLogging()`:
107
110
108
111
```csharp
@@ -187,11 +190,11 @@ Log.Logger = new LoggerConfiguration()
187
190
.CreateBootstrapLogger(); // <-- Change this line!
188
191
```
189
192
190
-
Then, pass a callback to `UseSerilog()` that creates the final logger:
193
+
Then, pass a callback to `AddSerilog()` that creates the final logger:
@@ -201,7 +204,7 @@ It's important to note that the final logger **completely replaces** the bootstr
201
204
202
205
#### Consuming `appsettings.json` configuration
203
206
204
-
**Using two-stage initialization**, insert the `ReadFrom.Configuration(context.Configuration)` call shown in the example above. The JSON configuration syntax is documented in [the _Serilog.Settings.Configuration_ README](https://github.com/serilog/serilog-settings-configuration).
207
+
**Using two-stage initialization**, insert the `ReadFrom.Configuration(builder.Configuration)` call shown in the example above. The JSON configuration syntax is documented in [the _Serilog.Settings.Configuration_ README](https://github.com/serilog/serilog-settings-configuration).
205
208
206
209
#### Injecting services into enrichers and sinks
207
210
@@ -213,20 +216,6 @@ It's important to note that the final logger **completely replaces** the bootstr
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()`.
219
-
220
-
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.
221
-
222
-
To have Serilog pass events to providers, **using two-stage initialization** as above, pass `writeToProviders: true` in the call to `UseSerilog()`:
The `Console()`, `Debug()`, and `File()` sinks all support JSON-formatted output natively, via the included _Serilog.Formatting.Compact_ package.
@@ -276,17 +265,13 @@ using (logger.BeginScope(new Dictionary<string, object>
276
265
}
277
266
```
278
267
279
-
The code above results in the same outcome as if you would push properties in the **ILogger** in Serilog.
268
+
The code above results in the same outcome as if you would push properties in the **LogContext** in Serilog. More details can be found in https://github.com/serilog/serilog/wiki/Enrichment#the-logcontext.
280
269
281
270
```csharp
282
-
// Serilog ILogger
283
-
using (logger.PushProperty("UserId", "svrooij"))
284
-
using (logger.PushProperty("OperationType", "update"))
271
+
// Serilog LogContext
272
+
using (LogContext.PushProperty("UserId", "svrooij"))
273
+
using (LogContext.PushProperty("OperationType", "update"))
285
274
{
286
275
// UserId and OperationType are set for all logging events in these brackets
287
276
}
288
277
```
289
-
290
-
### Versioning
291
-
292
-
This package tracks the versioning and target framework support of its (indirect) [_Microsoft.Extensions.Hosting_](https://nuget.org/packages/Microsoft.Extensions.Hosting) dependency.
0 commit comments