From fac8d45785f358fa694155ddba9d87954413ed0a Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Mon, 10 Nov 2025 12:46:32 +0100 Subject: [PATCH 1/3] updating docs --- .../enrichment/application-log-enricher.md | 45 +++++++++++++++++-- docs/core/enrichment/custom-enricher.md | 25 ++++++++++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index 2bf801420aecf..c7c5b31257bfb 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: diff --git a/docs/core/enrichment/custom-enricher.md b/docs/core/enrichment/custom-enricher.md index 4157f8cfef936..bb24aef7315f1 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, that doesn't change throughout the service's lifetime. + +```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(); +``` From fbdfe4555ddeb27dd4e50c9ea158abe721ba1e1a Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Mon, 10 Nov 2025 13:03:14 +0100 Subject: [PATCH 2/3] fix --- .../enrichment/application-log-enricher.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index c7c5b31257bfb..c2ece7c986776 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -165,8 +165,40 @@ Here's a complete example showing how to set up the service log enricher: **Program.cs:** +### [.NET 10+](#tab/net10-plus-config) + +```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-config) + :::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: From 36b5fcc79079589b7fa398e1b4810b2826e19af8 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Tue, 11 Nov 2025 10:15:02 +0100 Subject: [PATCH 3/3] fixing comments --- docs/core/enrichment/application-log-enricher.md | 4 ++-- docs/core/enrichment/custom-enricher.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index c2ece7c986776..cac10fc17b262 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -165,7 +165,7 @@ Here's a complete example showing how to set up the service log enricher: **Program.cs:** -### [.NET 10+](#tab/net10-plus-config) +### [.NET 10+](#tab/net10-plus-full-example) ```csharp using System.Text.Json; @@ -193,7 +193,7 @@ logger.LogInformation("This is a sample log message"); await host.RunAsync(); ``` -### [.NET 9 and earlier](#tab/net9-earlier-config) +### [.NET 9 and earlier](#tab/net9-earlier-full-example) :::code language="csharp" source="snippets/servicelogenricher/Program.cs" ::: diff --git a/docs/core/enrichment/custom-enricher.md b/docs/core/enrichment/custom-enricher.md index bb24aef7315f1..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 --- -## `ILogEnricher` 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 @@ -94,7 +94,7 @@ 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, that doesn't change throughout the service's lifetime. +.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