Skip to content

Commit 57d78d1

Browse files
authored
Merge pull request #378 from serilog/dev
8.0.2 Release
2 parents 68f2f3d + 1a88701 commit 57d78d1

File tree

11 files changed

+103
-153
lines changed

11 files changed

+103
-153
lines changed

README.md

+28-43
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Serilog logging for ASP.NET Core. This package routes ASP.NET Core log messages
44

55
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.
66

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.
811

912
### Instructions
1013

@@ -28,11 +31,9 @@ try
2831
Log.Information("Starting web application");
2932

3033
var builder = WebApplication.CreateBuilder(args);
31-
32-
builder.Host.UseSerilog(); // <-- Add this line
34+
builder.Services.AddSerilog(); // <-- Add this line
3335
3436
var app = builder.Build();
35-
3637
app.MapGet("/", () => "Hello World!");
3738

3839
app.Run();
@@ -47,23 +48,21 @@ finally
4748
}
4849
```
4950

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.
5152

5253
**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).
5354

5455
That's it! With the level bumped up a little you will see log output resembling:
5556

5657
```
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.
61+
[12:01:44 INF] Hosting environment: Development
62+
[12:01:44 INF] Content root path: serilog-aspnetcore/samples/Sample
63+
[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
6766
```
6867

6968
**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):
9796
}
9897
```
9998

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:
101100

102101
```csharp
103-
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
102+
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
103+
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
104+
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
104105
```
105106

107+
> **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+
106109
Then, in your application's _Program.cs_, add the middleware with `UseSerilogRequestLogging()`:
107110

108111
```csharp
@@ -187,11 +190,11 @@ Log.Logger = new LoggerConfiguration()
187190
.CreateBootstrapLogger(); // <-- Change this line!
188191
```
189192

190-
Then, pass a callback to `UseSerilog()` that creates the final logger:
193+
Then, pass a callback to `AddSerilog()` that creates the final logger:
191194

192195
```csharp
193-
builder.Host.UseSerilog((context, services, configuration) => configuration
194-
.ReadFrom.Configuration(context.Configuration)
196+
builder.Services.AddSerilog((services, lc) => lc
197+
.ReadFrom.Configuration(builder.Configuration)
195198
.ReadFrom.Services(services)
196199
.Enrich.FromLogContext()
197200
.WriteTo.Console());
@@ -201,7 +204,7 @@ It's important to note that the final logger **completely replaces** the bootstr
201204

202205
#### Consuming `appsettings.json` configuration
203206

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).
205208

206209
#### Injecting services into enrichers and sinks
207210

@@ -213,20 +216,6 @@ It's important to note that the final logger **completely replaces** the bootstr
213216
* `ILogEventSink`
214217
* `LoggingLevelSwitch`
215218

216-
#### Enabling `Microsoft.Extensions.Logging.ILoggerProvider`s
217-
218-
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()`:
223-
224-
```csharp
225-
builder.Host.UseSerilog(
226-
(hostingContext, services, loggerConfiguration) => /* snip! */,
227-
writeToProviders: true)
228-
```
229-
230219
### JSON output
231220

232221
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>
276265
}
277266
```
278267

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.
280269

281270
```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"))
285274
{
286275
// UserId and OperationType are set for all logging events in these brackets
287276
}
288277
```
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.

samples/Sample/Controllers/HomeController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public IActionResult Privacy()
3535
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
3636
public IActionResult Error()
3737
{
38-
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
38+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
3939
}
4040
}

samples/Sample/Program.cs

+60-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,69 @@
11
using Serilog;
2+
using Serilog.Events;
23
using Serilog.Templates;
4+
using Serilog.Templates.Themes;
35

4-
namespace Sample;
6+
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
7+
// logger configured in `AddSerilog()` below, once configuration and dependency-injection have both been
8+
// set up successfully.
9+
Log.Logger = new LoggerConfiguration()
10+
.WriteTo.Console()
11+
.CreateBootstrapLogger();
512

6-
public static class Program
13+
Log.Information("Starting up!");
14+
15+
try
716
{
8-
public static int Main(string[] args)
17+
var builder = WebApplication.CreateBuilder(args);
18+
19+
builder.Services.AddSerilog((services, lc) => lc
20+
.ReadFrom.Configuration(builder.Configuration)
21+
.ReadFrom.Services(services)
22+
.Enrich.FromLogContext()
23+
.WriteTo.Console(new ExpressionTemplate(
24+
// Include trace and span ids when present.
25+
"[{@t:HH:mm:ss} {@l:u3}{#if @tr is not null} ({substring(@tr,0,4)}:{substring(@sp,0,4)}){#end}] {@m}\n{@x}",
26+
theme: TemplateTheme.Code)));
27+
28+
builder.Services.AddControllersWithViews();
29+
30+
await using var app = builder.Build();
31+
32+
// Configure the HTTP request pipeline.
33+
if (!app.Environment.IsDevelopment())
934
{
10-
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
11-
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been
12-
// set up successfully.
13-
Log.Logger = new LoggerConfiguration()
14-
.WriteTo.Console()
15-
.CreateBootstrapLogger();
16-
17-
Log.Information("Starting up!");
18-
19-
try
20-
{
21-
CreateHostBuilder(args).Build().Run();
22-
23-
Log.Information("Stopped cleanly");
24-
return 0;
25-
}
26-
catch (Exception ex)
27-
{
28-
Log.Fatal(ex, "An unhandled exception occurred during bootstrapping");
29-
return 1;
30-
}
31-
finally
32-
{
33-
Log.CloseAndFlush();
34-
}
35+
app.UseExceptionHandler("/Home/Error");
36+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
37+
app.UseHsts();
3538
}
39+
40+
// Write streamlined request completion events, instead of the more verbose ones from the framework.
41+
// To use the default framework request logging instead, remove this line and set the "Microsoft"
42+
// level in appsettings.json to "Information".
43+
app.UseSerilogRequestLogging();
44+
45+
app.UseHttpsRedirection();
46+
app.UseStaticFiles();
47+
48+
app.UseRouting();
49+
50+
app.UseAuthorization();
51+
52+
app.MapControllerRoute(
53+
name: "default",
54+
pattern: "{controller=Home}/{action=Index}/{id?}");
3655

37-
public static IHostBuilder CreateHostBuilder(string[] args) =>
38-
Host.CreateDefaultBuilder(args)
39-
.UseSerilog((context, services, configuration) => configuration
40-
.ReadFrom.Configuration(context.Configuration)
41-
.ReadFrom.Services(services)
42-
.Enrich.FromLogContext()
43-
.WriteTo.Console(new ExpressionTemplate(
44-
// Include trace and span ids when present.
45-
"[{@t:HH:mm:ss} {@l:u3}{#if @tr is not null} ({substring(@tr,0,4)}:{substring(@sp,0,4)}){#end}] {@m}\n{@x}")))
46-
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
56+
await app.RunAsync();
57+
58+
Log.Information("Stopped cleanly");
59+
return 0;
60+
}
61+
catch (Exception ex)
62+
{
63+
Log.Fatal(ex, "An unhandled exception occurred during bootstrapping");
64+
return 1;
65+
}
66+
finally
67+
{
68+
Log.CloseAndFlush();
4769
}

samples/Sample/Sample.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<ProjectReference Include="..\..\src\Serilog.AspNetCore\Serilog.AspNetCore.csproj" />
99
</ItemGroup>
10-
10+
1111
<ItemGroup>
1212
<PackageReference Include="Serilog.Expressions" Version="4.0.0" />
1313
</ItemGroup>

samples/Sample/Startup.cs

-53
This file was deleted.

samples/Sample/appsettings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"MinimumLevel": {
44
"Default": "Information",
55
"Override": {
6-
"Microsoft": "Warning",
7-
"Microsoft.Hosting.Lifetime": "Information"
6+
"Microsoft.AspNetCore.Mvc": "Warning",
7+
"Microsoft.AspNetCore.Routing": "Warning",
8+
"Microsoft.AspNetCore.Hosting": "Warning"
89
}
910
},
1011
"WriteTo": [

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RequestLoggingMiddleware
3232
readonly Func<HttpContext, string, double, int, IEnumerable<LogEventProperty>> _getMessageTemplateProperties;
3333
readonly ILogger? _logger;
3434
readonly bool _includeQueryInRequestPath;
35-
static readonly LogEventProperty[] NoProperties = Array.Empty<LogEventProperty>();
35+
static readonly LogEventProperty[] NoProperties = [];
3636

3737
public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
3838
{
@@ -102,7 +102,7 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
102102
properties,
103103
traceId,
104104
spanId);
105-
105+
106106
logger.Write(evt);
107107

108108
return false;

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception? ex) =
3535
: LogEventLevel.Information;
3636

3737
static IEnumerable<LogEventProperty> DefaultGetMessageTemplateProperties(HttpContext httpContext, string requestPath, double elapsedMs, int statusCode) =>
38-
new[]
39-
{
38+
[
4039
new LogEventProperty("RequestMethod", new ScalarValue(httpContext.Request.Method)),
4140
new LogEventProperty("RequestPath", new ScalarValue(requestPath)),
4241
new LogEventProperty("StatusCode", new ScalarValue(statusCode)),
4342
new LogEventProperty("Elapsed", new ScalarValue(elapsedMs))
44-
};
43+
];
4544

4645
/// <summary>
4746
/// Gets or sets the message template. The default value is

src/Serilog.AspNetCore/Serilog.AspNetCore.csproj

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>Serilog support for ASP.NET Core logging</Description>
55
<!-- This must match the major and minor components of the referenced *.Extensions.* packages (and highest supported .NET TFM). -->
6-
<VersionPrefix>8.0.1</VersionPrefix>
6+
<VersionPrefix>8.0.2</VersionPrefix>
77
<Authors>Microsoft;Serilog Contributors</Authors>
88
<TargetFrameworks>net462;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -34,11 +34,8 @@
3434
<ItemGroup>
3535
<!-- The versions of all references in this group must match the major and minor components of the package version prefix. -->
3636
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
37-
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
38-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
37+
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
3938
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
40-
<!-- Temporary addition to pull in trace/span support -->
41-
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
4239
</ItemGroup>
4340

4441
<ItemGroup Condition=" '$(TargetFramework)' != 'net462' and '$(TargetFramework)' != 'netstandard2.0' ">

0 commit comments

Comments
 (0)