@@ -56,8 +56,8 @@ instrumentation.
56
56
We personally like [ Serilog] ( https://serilog.net ) and
57
57
the [ Serilog.AspNetCore] ( https://github.com/serilog/serilog-aspnetcore ) package a lot. Give it a try:
58
58
59
- ``` cs
60
- // Program.cs
59
+ ``` csharp
60
+ // Program.cs
61
61
Activity .DefaultIdFormat = ActivityIdFormat .W3C ;
62
62
63
63
Log .Logger = new LoggerConfiguration ()
@@ -71,4 +71,42 @@ Log.Logger = new LoggerConfiguration()
71
71
.CreateLogger ();
72
72
73
73
builder .Logging .AddSeriLog ();
74
- ```
74
+ ```
75
+
76
+ You can also use ASP.NET Core's configuration pattern to configure Serilog using ` appsettings.json ` and other configuration sources.
77
+ To do so, you first need to tell Serilog to read its configuration from the ` IConfiguration ` root:
78
+
79
+ ``` csharp {11}
80
+ // Program.cs
81
+
82
+ var builder = WebApplication .CreateBuilder (args );
83
+
84
+ builder .Host .UseSerilog ((ctx , lc ) => lc
85
+ .WriteTo .Console (
86
+ outputTemplate :
87
+ " [{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}" ,
88
+ formatProvider : CultureInfo .InvariantCulture )
89
+ .Enrich .FromLogContext ()
90
+ .ReadFrom .Configuration (ctx .Configuration ));
91
+ ```
92
+
93
+ Then, in your ` appsettings.json ` file, you can set the default minimum log level and log level overrides like so:
94
+
95
+ ``` json {12}
96
+ // appsettings.json
97
+
98
+ {
99
+ "Serilog" : {
100
+ "MinimumLevel" : {
101
+ "Default" : " Debug" ,
102
+ "Override" : {
103
+ "Microsoft" : " Warning" ,
104
+ "Microsoft.Hosting.Lifetime" : " Information" ,
105
+ "Microsoft.AspNetCore.Authentication" : " Debug" ,
106
+ "System" : " Warning" ,
107
+ "Duende" : " Verbose" // As an example, we've enabled more verbose logging for the Duende.* namespace
108
+ }
109
+ }
110
+ }
111
+ }
112
+ ```
0 commit comments