Skip to content

Commit cce8c85

Browse files
serilog-log-levels: update for .NET 10 rewrite
Retarget sample and tests to net10.0, bump Serilog to 4.4.0 and Serilog.AspNetCore to 10.0.0, and add a LoggingLevelSwitch example with a runtime admin endpoint to flip the minimum level without a redeploy.
1 parent 3b80c14 commit cce8c85

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Serilog" Version="2.12.0" />
11-
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
12-
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
13-
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
10+
<PackageReference Include="Serilog" Version="4.4.0" />
11+
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
12+
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
13+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
14+
1415

15-
1616
</ItemGroup>
1717

1818
</Project>

‎dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/Program.cs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Serilog;
2+
using Serilog.Core;
23
using Serilog.Events;
34

45
var configuration = new ConfigurationBuilder()
@@ -10,13 +11,17 @@
1011
Log.Information("Web Host started");
1112
var builder = WebApplication.CreateBuilder(args);
1213

14+
// Wrapping the minimum level in a LoggingLevelSwitch lets us change it at
15+
// runtime -- e.g. from an admin endpoint -- with no redeploy and no restart.
16+
var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Information);
17+
1318
builder.Services.AddSerilog(options =>
1419
{
1520
//we can configure serilog from configuration
1621
options.ReadFrom.Configuration(configuration);
1722

1823
//or we can configure serilog via fluent api
19-
options.MinimumLevel.Information()
24+
options.MinimumLevel.ControlledBy(levelSwitch)
2025
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
2126
.MinimumLevel.Override("System", LogEventLevel.Warning)
2227
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information,
@@ -44,4 +49,12 @@
4449
name: "default",
4550
pattern: "{controller=Home}/{action=Index}/{id?}");
4651

52+
// e.g. POST /admin/log-level/Debug -- flips the minimum level on every sink
53+
// immediately, with no restart.
54+
app.MapPost("/admin/log-level/{level}", (LogEventLevel level) =>
55+
{
56+
levelSwitch.MinimumLevel = level;
57+
return Results.Ok($"Minimum log level set to {level}.");
58+
});
59+
4760
app.Run();

‎dotnet-logging/LogLevelsWithSerilog/Tests/LogLevelsWithSerilogTests.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

0 commit comments

Comments
 (0)