Skip to content

Commit 0cf5905

Browse files
authored
Merge pull request #718 from DuendeSoftware/wca/diagnostics-logging
Added clarification on how to configure Serilog for more diagnostic logging
2 parents e31a3cd + 9f1b18b commit 0cf5905

File tree

1 file changed

+41
-3
lines changed
  • src/content/docs/identityserver/diagnostics

1 file changed

+41
-3
lines changed

src/content/docs/identityserver/diagnostics/logging.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ instrumentation.
5656
We personally like [Serilog](https://serilog.net) and
5757
the [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore) package a lot. Give it a try:
5858

59-
```cs
60-
//Program.cs
59+
```csharp
60+
// Program.cs
6161
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
6262

6363
Log.Logger = new LoggerConfiguration()
@@ -71,4 +71,42 @@ Log.Logger = new LoggerConfiguration()
7171
.CreateLogger();
7272

7373
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

Comments
 (0)