diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index 2bf801420aecf..cac10fc17b262 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -77,13 +77,38 @@ You can provide additional configuration via `appsettings.json`. There are two p #### 3. Register the service log enricher -Register the log enricher into the dependency injection container using : +Register the log enricher into the dependency injection container: + +### [.NET 10+](#tab/net10-plus) + +Starting with .NET 10, use the method: + +```csharp +serviceCollection.AddApplicationLogEnricher(); +``` + +You can enable or disable individual options of the enricher: + +```csharp +serviceCollection.AddApplicationLogEnricher(options => +{ + options.BuildVersion = true; + options.DeploymentRing = true; +}); +``` + +### [.NET 9 and earlier](#tab/net9-earlier) + +For .NET 9 and earlier versions, use the method: + +> [!WARNING] +> The `AddServiceLogEnricher` method is obsolete starting with .NET 10. Use `AddApplicationLogEnricher` instead. ```csharp serviceCollection.AddServiceLogEnricher(); ``` -You can enable or disable individual options of the enricher using : +You can enable or disable individual options of the enricher: ```csharp serviceCollection.AddServiceLogEnricher(options => @@ -93,18 +118,30 @@ serviceCollection.AddServiceLogEnricher(options => }); ``` +--- + Alternatively, configure options using `appsettings.json`: :::code language="json" source="snippets/servicelogenricher/appsettings.json" range="8-11"::: -And apply the configuration using : +And apply the configuration: + +### [.NET 10+](#tab/net10-plus-config) ```csharp var builder = Host.CreateApplicationBuilder(args); -builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); +builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); +``` +### [.NET 9 and earlier](#tab/net9-earlier-config) + +```csharp +var builder = Host.CreateApplicationBuilder(args); +builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); ``` +--- + ### `ApplicationLogEnricherOptions` Configuration options The service log enricher supports several configuration options through the class: @@ -128,8 +165,40 @@ Here's a complete example showing how to set up the service log enricher: **Program.cs:** +### [.NET 10+](#tab/net10-plus-full-example) + +```csharp +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +var builder = Host.CreateApplicationBuilder(args); +builder.UseApplicationMetadata(); +builder.Logging.EnableEnrichment(); +builder.Logging.AddJsonConsole(op => +{ + op.JsonWriterOptions = new JsonWriterOptions + { + Indented = true + }; +}); +builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); + +var host = builder.Build(); +var logger = host.Services.GetRequiredService>(); + +logger.LogInformation("This is a sample log message"); + +await host.RunAsync(); +``` + +### [.NET 9 and earlier](#tab/net9-earlier-full-example) + :::code language="csharp" source="snippets/servicelogenricher/Program.cs" ::: +--- + ### Enriched log output With the service log enricher configured, your log output will include service-specific dimensions: diff --git a/docs/core/enrichment/custom-enricher.md b/docs/core/enrichment/custom-enricher.md index 4157f8cfef936..4dd7ed8d3bb29 100644 --- a/docs/core/enrichment/custom-enricher.md +++ b/docs/core/enrichment/custom-enricher.md @@ -35,7 +35,7 @@ dotnet package add Microsoft.Extensions.Telemetry.Abstractions --- -## Implementation +## ILogEnricher implementation Your custom enricher only needs to implement a single method. During enrichment, this method is called and given an instance. The enricher then calls one of the overloads of @@ -91,3 +91,26 @@ var builder = Host.CreateApplicationBuilder(); builder.Logging.EnableEnrichment(); builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue")); ``` + +## Static log enrichers + +.NET provides to enrich logs with immutable properties throughout the application lifecycle. Static enrichers execute once during startup initialization, delivering significant performance benefits over standard enrichers that execute for each log record. This approach is particularly advantageous in high-throughput logging scenarios, as it eliminates the overhead of redundant enrichment operations for data that remains constant throughout the service's lifetime. Although executed only once at startup, the enrichment results are attached to every single log message produced by your application. + +```csharp +public class StaticEnricher : IStaticLogEnricher +{ + public void Enrich(IEnrichmentTagCollector collector) + { + collector.Add("app.version", "1.0.0"); + collector.Add("environment", "production"); + } +} +``` + +And you register it as shown in the following code : + +```csharp +var builder = Host.CreateApplicationBuilder(args); +builder.Logging.EnableEnrichment(); +builder.Services.AddStaticLogEnricher(); +```