Skip to content

Commit 3bde59e

Browse files
khalidabuhakmehpgermishuys
authored andcommitted
Add Microsoft.Extensions.Logging setup guide to IdentityServer diagnostics documentation.
1 parent 3193d68 commit 3bde59e

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

  • src/content/docs/identityserver/diagnostics

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,92 @@ In production, logging might produce too much data. It is recommended you either
5151
instrumentation.
5252
:::
5353

54+
### Setup for Microsoft.Extensions.Logging
55+
56+
.NET provides a logging abstraction interface found in the
57+
[Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging) package and is the default logging provider for ASP.NET Core.
58+
59+
If you prefer to use Microsoft's logging option,
60+
you can remove references to Serilog and fall back to the default logging implementation.
61+
Duende IdentityServer already uses the `ILogger` interface,
62+
and will use any implementation registered with the services collection.
63+
64+
Below you will find a modified version of the in-memory Duende IdentityServer sample.
65+
You can use it as a guide to adapt your own instance of Duende IdentityServer to use Microsoft's logging implementation..
66+
67+
```csharp
68+
using System.Globalization;
69+
using System.Text;
70+
using Duende.IdentityServer.Licensing;
71+
72+
// App1 contains WebApplicationBuilder extension methods
73+
// update according to your application's namespace
74+
using App1;
75+
76+
var builder = WebApplication.CreateBuilder(args);
77+
78+
var app = builder
79+
// WebApplicationBuilder extension methods
80+
.ConfigureServices()
81+
.ConfigurePipeline();
82+
83+
try
84+
{
85+
app.Logger.LogInformation("Starting up");
86+
87+
if (app.Environment.IsDevelopment())
88+
{
89+
app.Lifetime.ApplicationStopping.Register(() =>
90+
{
91+
var usage = app.Services.GetRequiredService<LicenseUsageSummary>();
92+
93+
app.Logger.LogInformation(Summary(usage));
94+
});
95+
}
96+
97+
app.Run();
98+
}
99+
catch (Exception ex) when (ex is not HostAbortedException)
100+
{
101+
app.Logger.LogCritical(ex, "Host terminated unexpectedly");
102+
}
103+
finally
104+
{
105+
app.Logger.LogInformation("Shut down complete");
106+
}
107+
108+
static string Summary(LicenseUsageSummary usage)
109+
{
110+
var sb = new StringBuilder();
111+
sb.AppendLine("IdentityServer Usage Summary:");
112+
sb.AppendLine(CultureInfo.InvariantCulture, $" License: {usage.LicenseEdition}");
113+
var features = usage.FeaturesUsed.Count > 0 ? string.Join(", ", usage.FeaturesUsed) : "None";
114+
sb.AppendLine(CultureInfo.InvariantCulture, $" Business and Enterprise Edition Features Used: {features}");
115+
sb.AppendLine(CultureInfo.InvariantCulture, $" {usage.ClientsUsed.Count} Client Id(s) Used");
116+
sb.AppendLine(CultureInfo.InvariantCulture, $" {usage.IssuersUsed.Count} Issuer(s) Used");
117+
118+
return sb.ToString();
119+
}
120+
```
121+
122+
You will also need to modify the `appSettings.json` file to include the `Logging` section:
123+
124+
```json
125+
{
126+
"Logging": {
127+
"LogLevel": {
128+
"Default": "Information",
129+
"Microsoft": "Warning",
130+
"Microsoft.Hosting.Lifetime": "Information",
131+
"Duende.IdentityServer": "Information"
132+
}
133+
},
134+
"AllowedHosts": "*"
135+
}
136+
```
137+
138+
Learn more about configuring logging in .NET applications by reading the [Microsoft documentation on logging fundamentals](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-9.0#configure-logging). As you'll see in the Microsoft documentation, configuring logging can be very involved and target different log levels, which can be useful for troubleshooting.
139+
54140
### Setup For Serilog
55141

56142
We personally like [Serilog](https://serilog.net) and

0 commit comments

Comments
 (0)