Skip to content

Commit 8ef7797

Browse files
authored
Merge pull request #866 from DuendeSoftware/ka/logging-microsoft
Add logger setup guide for IdentityServer diagnostics
2 parents 7ba8898 + 72ff093 commit 8ef7797

File tree

1 file changed

+94
-2
lines changed
  • src/content/docs/identityserver/diagnostics

1 file changed

+94
-2
lines changed

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

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,102 @@ 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

56-
We personally like [Serilog](https://serilog.net) and
57-
the [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore) package a lot. Give it a try:
142+
[Serilog](https://serilog.net) is a trusted and popular logging library for .NET applications.
143+
It is highly configurable,
144+
and at Duende, we think it is a **great alternative** to the default logging
145+
implementation,
146+
especially for .NET developers looking for more control over their logging configuration.
147+
Additionally,
148+
ASP.NET Core developers can use the [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore) for
149+
better integration with ASP.NET Core applications.
58150

59151
```csharp
60152
// Program.cs

0 commit comments

Comments
 (0)