|
| 1 | +--- |
| 2 | +title: "Logging Fundamentals" |
| 3 | +description: "General guidance on configuring logging for Duende Software products using Microsoft.Extensions.Logging and Serilog." |
| 4 | +date: 2026-01-19 |
| 5 | +sidebar: |
| 6 | + order: 10 |
| 7 | +--- |
| 8 | + |
| 9 | +All Duende Software products (IdentityServer, Backend for Frontend (BFF), Access Token Management, etc.) use the standard logging facilities provided by ASP.NET Core (`Microsoft.Extensions.Logging`). |
| 10 | +This means they integrate seamlessly with whatever logging provider you choose for your application. |
| 11 | + |
| 12 | +This guide provides general instructions for setting up logging that apply to all our products. |
| 13 | + |
| 14 | +## Log Levels |
| 15 | + |
| 16 | +We adhere to the standard Microsoft guidelines for log levels. Understanding these levels helps you configure the appropriate verbosity for your environment. |
| 17 | + |
| 18 | +* **`Trace`** |
| 19 | + * **Usage:** Extremely detailed information for troubleshooting complex issues. |
| 20 | + * **Production:** **Do not enable** in production unless specifically instructed for diagnostics. May contain sensitive data (e.g., token hashes, PII). |
| 21 | +* **`Debug`** |
| 22 | + * **Usage:** Internal flow details, useful for understanding _why_ a decision was made (e.g., policy evaluation, token validation steps). |
| 23 | + * **Production:** Generally disabled in production, but safe to enable temporarily for deeper investigation. |
| 24 | +* **`Information`** |
| 25 | + * **Usage:** High-level events tracking the general flow (e.g., "Request started", "Token issued"). |
| 26 | + * **Production:** Often the default level for production. |
| 27 | +* **`Warning`** |
| 28 | + * **Usage:** Unexpected events that didn't stop the application but might require investigation (e.g., "Invalid client configuration detected"). |
| 29 | +* **`Error`** |
| 30 | + * **Usage:** Exceptions and errors that cannot be handled gracefully. |
| 31 | +* **`Critical`** |
| 32 | + * **Usage:** Failures that require immediate attention (e.g., "Signing key not found"). |
| 33 | + |
| 34 | +## Setup for Microsoft.Extensions.Logging |
| 35 | + |
| 36 | +This is the default logging provider for ASP.NET Core. If you haven't configured a third-party logger, this is what you are using. |
| 37 | + |
| 38 | +You can configure log levels in your `appsettings.json` file. To get detailed logs from Duende products, you often want to set the `Duende` namespace (or specific sub-namespaces) to `Debug`. |
| 39 | + |
| 40 | +```json |
| 41 | +// appsettings.json |
| 42 | +{ |
| 43 | + "Logging": { |
| 44 | + "LogLevel": { |
| 45 | + "Default": "Information", |
| 46 | + "Microsoft": "Warning", |
| 47 | + "Microsoft.Hosting.Lifetime": "Information", |
| 48 | + // Enable Debug logs for all Duende products |
| 49 | + "Duende": "Debug" |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Setup for Serilog |
| 56 | + |
| 57 | +[Serilog](https://serilog.net) is a popular structured logging library for .NET. We highly recommend it for its flexibility and rich sink ecosystem (Console, File, Seq, Elasticsearch, etc.). |
| 58 | + |
| 59 | +### 1. Installation |
| 60 | + |
| 61 | +Install the necessary packages: |
| 62 | + |
| 63 | +```bash |
| 64 | +dotnet add package Serilog.AspNetCore |
| 65 | +``` |
| 66 | + |
| 67 | +### 2. Configuration In `Program.cs` |
| 68 | + |
| 69 | +Configure Serilog early in your application startup to capture all logs, including startup errors. |
| 70 | + |
| 71 | +```csharp |
| 72 | +// Program.cs |
| 73 | +using Serilog; |
| 74 | + |
| 75 | +var builder = WebApplication.CreateBuilder(args); |
| 76 | + |
| 77 | +// Configure Serilog |
| 78 | +builder.Host.UseSerilog((ctx, lc) => lc |
| 79 | + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") |
| 80 | + .Enrich.FromLogContext() |
| 81 | + .ReadFrom.Configuration(ctx.Configuration)); |
| 82 | + |
| 83 | +var app = builder.Build(); |
| 84 | + |
| 85 | +app.UseSerilogRequestLogging(); // Optional: cleaner HTTP request logging |
| 86 | +
|
| 87 | +// ... rest of your pipeline |
| 88 | +``` |
| 89 | + |
| 90 | +### 3. Configuration In `appsettings.json` |
| 91 | + |
| 92 | +You can then control log levels via `appsettings.json`. This approach allows you to change log levels without recompiling your code. |
| 93 | + |
| 94 | +```json |
| 95 | +{ |
| 96 | + "Serilog": { |
| 97 | + "MinimumLevel": { |
| 98 | + "Default": "Information", |
| 99 | + "Override": { |
| 100 | + "Microsoft": "Warning", |
| 101 | + "Microsoft.Hosting.Lifetime": "Information", |
| 102 | + "System": "Warning", |
| 103 | + // Enable detailed logging for Duende products |
| 104 | + "Duende": "Debug" |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Troubleshooting Specific Products |
| 112 | + |
| 113 | +If you are debugging a specific component, you can target its namespace to reduce noise. |
| 114 | + |
| 115 | +| Product | Namespace | |
| 116 | +|-----------------------------|--------------------------------| |
| 117 | +| **IdentityServer** | `Duende.IdentityServer` | |
| 118 | +| **BFF** | `Duende.Bff` | |
| 119 | +| **Access Token Management** | `Duende.AccessTokenManagement` | |
| 120 | + |
| 121 | +Example `appsettings.json` for debugging only BFF interactions: |
| 122 | + |
| 123 | +```json |
| 124 | +"Duende.Bff": "Debug", |
| 125 | +"Duende.IdentityServer": "Information" |
| 126 | +``` |
0 commit comments