File tree Expand file tree Collapse file tree
dotnet-logging/LogLevelsWithSerilog Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11using Serilog ;
2+ using Serilog . Core ;
23using Serilog . Events ;
34
45var configuration = new ConfigurationBuilder ( )
1011Log . Information ( "Web Host started" ) ;
1112var 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+
1318builder . 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 ,
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+
4760app . Run ( ) ;
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments