From d43591836cd60cf4235ae1eaaa4a2ea00670a38a Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 13:27:54 +0200 Subject: [PATCH 01/14] adding docs --- docs/core/enrichment/application-enricher.md | 133 ++++++++++++++++++ docs/core/enrichment/application-metadata.md | 94 +++++++++++++ .../servicelogenricher/output-full.json | 14 ++ .../servicelogenricher/Program.cs | 31 ++++ .../servicelogenricher/appsettings.json | 12 ++ .../servicelogenricher.csproj | 24 ++++ 6 files changed, 308 insertions(+) create mode 100644 docs/core/enrichment/application-enricher.md create mode 100644 docs/core/enrichment/application-metadata.md create mode 100644 docs/core/enrichment/snippets/servicelogenricher/output-full.json create mode 100644 docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs create mode 100644 docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json create mode 100644 docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj diff --git a/docs/core/enrichment/application-enricher.md b/docs/core/enrichment/application-enricher.md new file mode 100644 index 0000000000000..92876dcf28d51 --- /dev/null +++ b/docs/core/enrichment/application-enricher.md @@ -0,0 +1,133 @@ +--- +title: Application enricher +description: Learn how to use the application log enricher to add application-specific information to your telemetry in .NET. +ms.date: 10/14/2025 +--- + +# Application enricher + +The application log enricher augments telemetry logs with application-specific information such as service host details and application metadata. This enricher provides essential context about your application's deployment environment, version information, and service identity that helps with monitoring, debugging, and operational visibility. + +You can register the enrichers in an IoC container, and all registered enrichers are automatically picked up by respective telemetry logs, where they enrich the telemetry information. + +## Prerequisites + +To function properly, this enricher requires that [application metadata](xref:application-metadata) is configured and available. The application metadata provides the foundational information that the enricher uses to populate telemetry dimensions. + +## Install the package + +To get started, install the [📦 Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) NuGet package: + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Microsoft.Extensions.Telemetry +``` + +Or, if you're using .NET 10+ SDK: + +```dotnetcli +dotnet package add Microsoft.Extensions.Telemetry +``` + +## Application log enricher + +The application log enricher provides application-specific enrichment. The log enricher specifically targets log telemetry and adds standardized dimensions that help identify and categorize log entries by service characteristics. + +### Step-by-step configuration + +Follow these steps to configure the application log enricher in your application: + +#### 1. Configure Application Metadata + +First, configure the [Application Metadata](xref:application-metadata) by calling the method: + +```csharp +var builder = Host.CreateDefaultBuilder(); +builder.UseApplicationMetadata() +``` + +This method automatically picks up values from the and saves them to the default configuration section `ambientmetadata:application`. + +Alternatively, you can use this method , which registers a configuration provider for application metadata by picking up the values from the and adds it to the given configuration section name. Then you use method to register the metadata in the dependency injection container, which allow you to pass separately: + +```csharp +var hostBuilder = Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((context, builder) => + builder.AddApplicationMetadata(context.HostingEnvironment)) + .ConfigureServices((context, services) => + services.AddApplicationMetadata(context.Configuration.GetSection("ambientmetadata:application"))); +``` + +#### 2. Provide additional configuration (optional) + +You can provide additional configuration via `appsettings.json`. There are two properties in the [Application Metadata](xref:application-metadata) that don't get values automatically: `BuildVersion` and `DeploymentRing`. If you want to use them, provide values manually: + +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: + +#### 3. Register the service log enricher + +Register the log enricher into the dependency injection container using : + +```csharp +serviceCollection.AddServiceLogEnricher(); +``` + +You can enable or disable individual options of the enricher using : + +```csharp +serviceCollection.AddServiceLogEnricher(options => +{ + options.BuildVersion = true; + options.DeploymentRing = true; +}); +``` + +Alternatively, configure options using `appsettings.json`: + +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="9-12"::: + +And apply the configuration using : + +```csharp +var builder = Host.CreateDefaultBuilder(args); +builder.ConfigureServices((context, services) => +{ + services.AddServiceLogEnricher(context.Configuration.GetSection("applicationlogenricheroptions")); +}); +``` + +### `ApplicationLogEnricherOptions` Configuration options + +The service log enricher supports several configuration options through the class: + +| Property | Default Value | Dimension Name | Description | +|----------|---------------|----------------|-------------| +| `EnvironmentName` | true | `deployment.environment` | Environment name from hosting environment or configuration | +| `ApplicationName` | true | `service.name` | Application name from hosting environment or configuration | +| `BuildVersion` | false | `service.version` | Build version from configuration | +| `DeploymentRing` | false | `DeploymentRing` | Deployment ring from configuration | + +By default, the enricher includes `EnvironmentName` and `ApplicationName` in log entries. The `BuildVersion` and `DeploymentRing` properties are disabled by default and must be explicitly enabled if needed. + +### Complete example + +Here's a complete example showing how to set up the service log enricher: + +**appsettings.json:** + +:::code language="json" source="snippets/servicelogenricher/appsettings.json"::: + +**Program.cs:** + +:::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: + +:::code language="csharp" source="snippets/servicelogenricher/output-full.json" highlight="9-11" ::: + +## Next steps + +- Learn about [application metadata configuration](xref:application-metadata) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md new file mode 100644 index 0000000000000..bf8519488d612 --- /dev/null +++ b/docs/core/enrichment/application-metadata.md @@ -0,0 +1,94 @@ +--- +title: Application metada +description: Learn how to use the application metadata to add service-specific information to your service in .NET. +ms.date: 10/14/2025 +--- + +# Application metadata + +The [Application metadata provider](xref:Microsoft.Extensions.Configuration.ApplicationMetadataConfigurationBuilderExtensions.AddApplicationMetadata*) supplies service runtime information for application-level ambient metadata such as the version, deployment ring, environment, and name. This information can be useful to enrich any telemetry your service is emitting. + +## Install the package + +To get started, install the [📦 Microsoft.Extensions.AmbientMetadata.Application](https://www.nuget.org/packages/Microsoft.Extensions.AmbientMetadata.Application) NuGet package: + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Microsoft.Extensions.AmbientMetadata.Application +``` + +Or, if you're using .NET 10+ SDK: + +```dotnetcli +dotnet package add Microsoft.Extensions.AmbientMetadata.Application +``` + +### [PackageReference](#tab/package-reference) + +```xml + +``` + +The following shows the information made available by the provider via : + +| Key | Required? | Where the value comes from| Value Example | Description +|-|-|-|-| +| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name. +| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Staging`, `Development` | The environment the application is deployed to. +| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version. +| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running. + +## Example + +To use this provider, you need to use the method, +which populates `ApplicationName` and `EnvironmentName` values automatically from `IHostEnvironment`. +Optionally, you can provide values for `BuildVersion` and `DeploymentRing` via the `appsettings.json` file. +The complete example is as follows: + +`appsettings.json`: + +```json +{ + "ambientmetadata": { + "application": { + "buildversion": "1.0.0.0", // provide a build version. + "deploymentring": "deploymentRing" // provide a deployment ring value. + } + } +} +``` + +```cs +using var host = await new HostBuilder() + // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` after calling the method below: + .UseApplicationMetadata() + .ConfigureAppConfiguration(builder => + { + // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. + _ = builder.AddJsonFile("appsettings.json"); + }) + .Build() + .StartAsync(); + +// work with metadata options +var metadataOptions = host.Services.GetRequiredService>(); +var buildVersion = metadataOptions.Value.BuildVersion; +``` + +Alternatively, you can achieve the same result as above by doing this: + +```cs +using var hostBuilder = new HostBuilder() + .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => configurationBuilder + .AddApplicationMetadata(hostBuilderContext.HostingEnvironment) + .AddJsonFile("appsettings.json")) + .ConfigureServices((hostBuilderContext, serviceCollection) => serviceCollection + .AddApplicationMetadata(hostBuilderContext.Configuration.GetSection("ambientmetadata:application"))) + .Build(); + +// work with metadata options +var metadataOptions = host.Services.GetRequiredService>(); +var buildVersion = metadataOptions.Value.BuildVersion; +``` diff --git a/docs/core/enrichment/snippets/servicelogenricher/output-full.json b/docs/core/enrichment/snippets/servicelogenricher/output-full.json new file mode 100644 index 0000000000000..422cb4c0489db --- /dev/null +++ b/docs/core/enrichment/snippets/servicelogenricher/output-full.json @@ -0,0 +1,14 @@ +{ + "EventId": 0, + "LogLevel": "Information", + "Category": "Program", + "Message": "This is a sample log message", + "State": { + "Message": "This is a sample log message", + "service.name": "servicelogenricher", + "deployment.environment": "Production", + "DeploymentRing": "testring", + "service.version": "1.2.3", + "{OriginalFormat}": "This is a sample log message" + } +} \ No newline at end of file diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs new file mode 100644 index 0000000000000..90c3329950e4e --- /dev/null +++ b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +var builder = Host.CreateDefaultBuilder(args); +builder.UseApplicationMetadata(); +builder.ConfigureLogging(builder => +{ + builder.EnableEnrichment(); + builder.AddJsonConsole(op => + { + op.JsonWriterOptions = new JsonWriterOptions + { + Indented = true + }; + }); +}); +builder.ConfigureServices((context, services) => +{ + services.AddServiceLogEnricher(context.Configuration.GetSection("applicationlogenricheroptions")); +}); + +var host = builder.Build(); +var logger = host.Services.GetRequiredService>(); + +logger.LogInformation("This is a sample log message"); + +await host.RunAsync(); + + diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json new file mode 100644 index 0000000000000..263f4b20b705c --- /dev/null +++ b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json @@ -0,0 +1,12 @@ +{ + "AmbientMetadata": { + "Application": { + "DeploymentRing": "testring", + "BuildVersion": "1.2.3" + } + }, + "ApplicationLogEnricherOptions": { + "BuildVersion": true, + "DeploymentRing": true + } +} diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj new file mode 100644 index 0000000000000..7019c1d302f00 --- /dev/null +++ b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj @@ -0,0 +1,24 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + + + + + PreserveNewest + + + + From d0a102a7ea22200dcc2e214b18791d7cf38db4ee Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 13:43:28 +0200 Subject: [PATCH 02/14] fixes --- ...ion-enricher.md => application-log-enricher.md} | 10 +++++----- docs/core/enrichment/application-metadata.md | 14 +++++++------- .../{servicelogenricher => }/Program.cs | 2 +- .../{servicelogenricher => }/appsettings.json | 0 .../servicelogenricher.csproj | 0 docs/navigate/tools-diagnostics/toc.yml | 4 ++++ 6 files changed, 17 insertions(+), 13 deletions(-) rename docs/core/enrichment/{application-enricher.md => application-log-enricher.md} (96%) rename docs/core/enrichment/snippets/servicelogenricher/{servicelogenricher => }/Program.cs (95%) rename docs/core/enrichment/snippets/servicelogenricher/{servicelogenricher => }/appsettings.json (100%) rename docs/core/enrichment/snippets/servicelogenricher/{servicelogenricher => }/servicelogenricher.csproj (100%) diff --git a/docs/core/enrichment/application-enricher.md b/docs/core/enrichment/application-log-enricher.md similarity index 96% rename from docs/core/enrichment/application-enricher.md rename to docs/core/enrichment/application-log-enricher.md index 92876dcf28d51..1e54bb4c51a43 100644 --- a/docs/core/enrichment/application-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -1,10 +1,10 @@ --- -title: Application enricher +title: Application log enricher description: Learn how to use the application log enricher to add application-specific information to your telemetry in .NET. ms.date: 10/14/2025 --- -# Application enricher +# Application log enricher The application log enricher augments telemetry logs with application-specific information such as service host details and application metadata. This enricher provides essential context about your application's deployment environment, version information, and service identity that helps with monitoring, debugging, and operational visibility. @@ -73,7 +73,7 @@ Register the log enricher into the dependency injection container using : +You can enable or disable individual options of the enricher using : ```csharp serviceCollection.AddServiceLogEnricher(options => @@ -93,7 +93,7 @@ And apply the configuration using { - services.AddServiceLogEnricher(context.Configuration.GetSection("applicationlogenricheroptions")); + services.AddServiceLogEnricher(context.Configuration.GetSection("ApplicationLogEnricherOptions")); }); ``` @@ -126,7 +126,7 @@ Here's a complete example showing how to set up the service log enricher: With the service log enricher configured, your log output will include service-specific dimensions: -:::code language="csharp" source="snippets/servicelogenricher/output-full.json" highlight="9-11" ::: +:::code language="json" source="snippets/servicelogenricher/output-full.json" highlight="9-11" ::: ## Next steps diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index bf8519488d612..5dc680b202522 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -1,5 +1,5 @@ --- -title: Application metada +title: Application metadata description: Learn how to use the application metadata to add service-specific information to your service in .NET. ms.date: 10/14/2025 --- @@ -33,12 +33,12 @@ dotnet package add Microsoft.Extensions.AmbientMetadata.Application The following shows the information made available by the provider via : -| Key | Required? | Where the value comes from| Value Example | Description -|-|-|-|-| -| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name. -| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Staging`, `Development` | The environment the application is deployed to. -| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version. -| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running. +| Key | Required? | Where the value comes from| Value Example | Description| +|-|-|-|-|-| +| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name.| +| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Staging`, `Development` | The environment the application is deployed to.| +| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version.| +| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running.| ## Example diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs b/docs/core/enrichment/snippets/servicelogenricher/Program.cs similarity index 95% rename from docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs rename to docs/core/enrichment/snippets/servicelogenricher/Program.cs index 90c3329950e4e..7330d86457c4d 100644 --- a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/Program.cs +++ b/docs/core/enrichment/snippets/servicelogenricher/Program.cs @@ -18,7 +18,7 @@ }); builder.ConfigureServices((context, services) => { - services.AddServiceLogEnricher(context.Configuration.GetSection("applicationlogenricheroptions")); + services.AddServiceLogEnricher(context.Configuration.GetSection("ApplicationLogEnricherOptions")); }); var host = builder.Build(); diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json b/docs/core/enrichment/snippets/servicelogenricher/appsettings.json similarity index 100% rename from docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/appsettings.json rename to docs/core/enrichment/snippets/servicelogenricher/appsettings.json diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj similarity index 100% rename from docs/core/enrichment/snippets/servicelogenricher/servicelogenricher/servicelogenricher.csproj rename to docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 6074077eebbdf..c9a3ddc0e8082 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -450,6 +450,10 @@ items: items: - name: Overview href: ../../core/enrichment/overview.md + - name: Application metadata + href: ../../core/enrichment/application-metadata.md + - name: Application log enricher + href: ../../core/enrichment/application-log-enricher.md - name: Process log enricher href: ../../core/enrichment/process-log-enricher.md - name: Custom enricher From 3a519258301b36111531405dd9bfd3e71d6382c1 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 13:58:10 +0200 Subject: [PATCH 03/14] fixing warning --- .../enrichment/application-log-enricher.md | 10 +++--- docs/core/enrichment/application-metadata.md | 34 +++++-------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index 1e54bb4c51a43..88af18808fa42 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -12,7 +12,7 @@ You can register the enrichers in an IoC container, and all registered enrichers ## Prerequisites -To function properly, this enricher requires that [application metadata](xref:application-metadata) is configured and available. The application metadata provides the foundational information that the enricher uses to populate telemetry dimensions. +To function properly, this enricher requires that [application metadata](xref:application-metadata.md) is configured and available. The application metadata provides the foundational information that the enricher uses to populate telemetry dimensions. ## Install the package @@ -40,7 +40,7 @@ Follow these steps to configure the application log enricher in your application #### 1. Configure Application Metadata -First, configure the [Application Metadata](xref:application-metadata) by calling the method: +First, configure the [Application Metadata](xref:application-metadata.md) by calling the method: ```csharp var builder = Host.CreateDefaultBuilder(); @@ -61,13 +61,13 @@ var hostBuilder = Host.CreateDefaultBuilder() #### 2. Provide additional configuration (optional) -You can provide additional configuration via `appsettings.json`. There are two properties in the [Application Metadata](xref:application-metadata) that don't get values automatically: `BuildVersion` and `DeploymentRing`. If you want to use them, provide values manually: +You can provide additional configuration via `appsettings.json`. There are two properties in the [Application Metadata](xref:application-metadata.md) that don't get values automatically: `BuildVersion` and `DeploymentRing`. If you want to use them, provide values manually: :::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: #### 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 using : ```csharp serviceCollection.AddServiceLogEnricher(); @@ -130,4 +130,4 @@ With the service log enricher configured, your log output will include service-s ## Next steps -- Learn about [application metadata configuration](xref:application-metadata) +- Learn about [application metadata configuration](xref:application-metadata.md) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 5dc680b202522..7b5531fa72449 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -42,33 +42,18 @@ The following shows the information made available by the provider via method, +To use this provider, you need to use the method, which populates `ApplicationName` and `EnvironmentName` values automatically from `IHostEnvironment`. Optionally, you can provide values for `BuildVersion` and `DeploymentRing` via the `appsettings.json` file. -The complete example is as follows: - -`appsettings.json`: - -```json -{ - "ambientmetadata": { - "application": { - "buildversion": "1.0.0.0", // provide a build version. - "deploymentring": "deploymentRing" // provide a deployment ring value. - } - } -} -``` + +Your `appsettings.json` should have a section as follows : + +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: ```cs -using var host = await new HostBuilder() - // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` after calling the method below: +var host = Host.CreateDefaultBuilder() + // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` and BuildVersion and DeploymentRing will be imported from the "appsettings.json" file.: .UseApplicationMetadata() - .ConfigureAppConfiguration(builder => - { - // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. - _ = builder.AddJsonFile("appsettings.json"); - }) .Build() .StartAsync(); @@ -80,10 +65,9 @@ var buildVersion = metadataOptions.Value.BuildVersion; Alternatively, you can achieve the same result as above by doing this: ```cs -using var hostBuilder = new HostBuilder() +var hostBuilder = Host.CreateDefaultBuilder() .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => configurationBuilder - .AddApplicationMetadata(hostBuilderContext.HostingEnvironment) - .AddJsonFile("appsettings.json")) + .AddApplicationMetadata(hostBuilderContext.HostingEnvironment)) .ConfigureServices((hostBuilderContext, serviceCollection) => serviceCollection .AddApplicationMetadata(hostBuilderContext.Configuration.GetSection("ambientmetadata:application"))) .Build(); From 210e079ede610b661b47ec0664d3627d72861336 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 14:11:58 +0200 Subject: [PATCH 04/14] fixed reference --- docs/core/enrichment/application-log-enricher.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index 88af18808fa42..fb4d75671952e 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -12,7 +12,7 @@ You can register the enrichers in an IoC container, and all registered enrichers ## Prerequisites -To function properly, this enricher requires that [application metadata](xref:application-metadata.md) is configured and available. The application metadata provides the foundational information that the enricher uses to populate telemetry dimensions. +To function properly, this enricher requires that [application metadata](application-metadata.md) is configured and available. The application metadata provides the foundational information that the enricher uses to populate telemetry dimensions. ## Install the package @@ -40,7 +40,7 @@ Follow these steps to configure the application log enricher in your application #### 1. Configure Application Metadata -First, configure the [Application Metadata](xref:application-metadata.md) by calling the method: +First, configure the [Application Metadata](application-metadata.md) by calling the method: ```csharp var builder = Host.CreateDefaultBuilder(); @@ -61,7 +61,7 @@ var hostBuilder = Host.CreateDefaultBuilder() #### 2. Provide additional configuration (optional) -You can provide additional configuration via `appsettings.json`. There are two properties in the [Application Metadata](xref:application-metadata.md) that don't get values automatically: `BuildVersion` and `DeploymentRing`. If you want to use them, provide values manually: +You can provide additional configuration via `appsettings.json`. There are two properties in the [Application Metadata](application-metadata.md) that don't get values automatically: `BuildVersion` and `DeploymentRing`. If you want to use them, provide values manually: :::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: @@ -130,4 +130,4 @@ With the service log enricher configured, your log output will include service-s ## Next steps -- Learn about [application metadata configuration](xref:application-metadata.md) +- Learn about [application metadata configuration](application-metadata.md) From 95cdedd5e83655cab6a93700464f503c2fd963c2 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 14:20:39 +0200 Subject: [PATCH 05/14] fixes --- docs/core/enrichment/application-log-enricher.md | 13 +++++++++++-- docs/core/enrichment/application-metadata.md | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index fb4d75671952e..ad790b5363c78 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -30,6 +30,15 @@ Or, if you're using .NET 10+ SDK: dotnet package add Microsoft.Extensions.Telemetry ``` +### [PackageReference](#tab/package-reference) + +```xml + +``` + +--- + ## Application log enricher The application log enricher provides application-specific enrichment. The log enricher specifically targets log telemetry and adds standardized dimensions that help identify and categorize log entries by service characteristics. @@ -85,7 +94,7 @@ serviceCollection.AddServiceLogEnricher(options => Alternatively, configure options using `appsettings.json`: -:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="9-12"::: +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="8-11"::: And apply the configuration using : @@ -126,7 +135,7 @@ Here's a complete example showing how to set up the service log enricher: With the service log enricher configured, your log output will include service-specific dimensions: -:::code language="json" source="snippets/servicelogenricher/output-full.json" highlight="9-11" ::: +:::code language="json" source="snippets/servicelogenricher/output-full.json" highlight="8-11" ::: ## Next steps diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 7b5531fa72449..d9d7019749f34 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -31,6 +31,8 @@ dotnet package add Microsoft.Extensions.AmbientMetadata.Application Version="*" /> ``` +--- + The following shows the information made available by the provider via : | Key | Required? | Where the value comes from| Value Example | Description| From e57773c172a01ba7724cecd6c7856780cd58162b Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Wed, 22 Oct 2025 14:27:47 +0200 Subject: [PATCH 06/14] fixes --- docs/core/enrichment/application-metadata.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index d9d7019749f34..c85a48f46fba3 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -54,12 +54,12 @@ Your `appsettings.json` should have a section as follows : ```cs var host = Host.CreateDefaultBuilder() - // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` and BuildVersion and DeploymentRing will be imported from the "appsettings.json" file.: + // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` + // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. .UseApplicationMetadata() .Build() .StartAsync(); -// work with metadata options var metadataOptions = host.Services.GetRequiredService>(); var buildVersion = metadataOptions.Value.BuildVersion; ``` @@ -74,7 +74,6 @@ var hostBuilder = Host.CreateDefaultBuilder() .AddApplicationMetadata(hostBuilderContext.Configuration.GetSection("ambientmetadata:application"))) .Build(); -// work with metadata options var metadataOptions = host.Services.GetRequiredService>(); var buildVersion = metadataOptions.Value.BuildVersion; ``` From d77bbd79ec88ff87e6505927c7235770da76bfdc Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 15:36:01 +0200 Subject: [PATCH 07/14] fixes --- .../enrichment/application-log-enricher.md | 14 +- docs/core/enrichment/application-metadata.md | 205 ++++++++++++++++-- .../snippets/servicelogenricher/Program.cs | 22 +- .../servicelogenricher.csproj | 10 +- 4 files changed, 204 insertions(+), 47 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index ad790b5363c78..e67b36333bff4 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -49,10 +49,10 @@ Follow these steps to configure the application log enricher in your application #### 1. Configure Application Metadata -First, configure the [Application Metadata](application-metadata.md) by calling the method: +First, configure the [Application Metadata](application-metadata.md) by calling the methods: ```csharp -var builder = Host.CreateDefaultBuilder(); +var builder = Host.CreateApplicationBuilder(); builder.UseApplicationMetadata() ``` @@ -61,7 +61,7 @@ This method automatically picks up values from the , which registers a configuration provider for application metadata by picking up the values from the and adds it to the given configuration section name. Then you use method to register the metadata in the dependency injection container, which allow you to pass separately: ```csharp -var hostBuilder = Host.CreateDefaultBuilder() +var hostBuilder = Host.CreateApplicationBuilder() .ConfigureAppConfiguration((context, builder) => builder.AddApplicationMetadata(context.HostingEnvironment)) .ConfigureServices((context, services) => @@ -99,11 +99,9 @@ Alternatively, configure options using `appsettings.json`: And apply the configuration using : ```csharp -var builder = Host.CreateDefaultBuilder(args); -builder.ConfigureServices((context, services) => -{ - services.AddServiceLogEnricher(context.Configuration.GetSection("ApplicationLogEnricherOptions")); -}); +var builder = Host.CreateApplicationBuilder(args); +builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); + ``` ### `ApplicationLogEnricherOptions` Configuration options diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index c85a48f46fba3..51e55a2401fca 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -4,9 +4,19 @@ description: Learn how to use the application metadata to add service-specific i ms.date: 10/14/2025 --- -# Application metadata +# Application ambient metadata -The [Application metadata provider](xref:Microsoft.Extensions.Configuration.ApplicationMetadataConfigurationBuilderExtensions.AddApplicationMetadata*) supplies service runtime information for application-level ambient metadata such as the version, deployment ring, environment, and name. This information can be useful to enrich any telemetry your service is emitting. +The [`Microsoft.Extensions.AmbientMetadata.Application`](https://www.nuget.org/packages/Microsoft.Extensions.AmbientMetadata.Application) NuGet package provides functionality to capture and flow application-level ambient metadata throughout your application. This metadata includes information such as the application name, version, deployment environment, and deployment ring, which is valuable for enriching telemetry, troubleshooting, and analysis. + +## Why use application metadata + +Application metadata provides essential context about your running application that can enhance observability: + +- **Telemetry enrichment**: Automatically add application details to logs, metrics, and traces. +- **Troubleshooting**: Quickly identify which version of your application is experiencing issues. +- **Environment identification**: Distinguish between different environments in your telemetry. +- **Deployment tracking**: Track issues across different deployment rings or regions. +- **Consistent metadata**: Ensure all components in your application use the same metadata values. ## Install the package @@ -33,32 +43,49 @@ dotnet package add Microsoft.Extensions.AmbientMetadata.Application --- -The following shows the information made available by the provider via : +## Configure application metadata -| Key | Required? | Where the value comes from| Value Example | Description| -|-|-|-|-|-| -| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name.| -| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Staging`, `Development` | The environment the application is deployed to.| -| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version.| -| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running.| +Application metadata can be configured through your application's configuration system. The package looks for metadata under the `ambientmetadata:application` configuration section by default. + +### Configure with appsettings.json -## Example +Add the application metadata to your `appsettings.json` file: -To use this provider, you need to use the method, -which populates `ApplicationName` and `EnvironmentName` values automatically from `IHostEnvironment`. +```json +{ + "ambientmetadata": { + "application": { + "ApplicationName": "MyWebApi", + "BuildVersion": "1.0.0", + "DeploymentRing": "Production", + "EnvironmentName": "Production" + } + } +} +``` + +### Configure with IHostBuilder + +Use the extensions method to register application metadata, which populates `ApplicationName` and `EnvironmentName` values automatically from `IHostEnvironment`. Optionally, you can provide values for `BuildVersion` and `DeploymentRing` via the `appsettings.json` file. -Your `appsettings.json` should have a section as follows : +The following table shows the metadata made available by the provider via : -:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: +| Key | Required? | Where the value comes from| Value Example | Description| +|-|-|-|-|-| +| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name.| +| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Development`| The environment the application is deployed to.| +| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version.| +| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running.| ```cs -var host = Host.CreateDefaultBuilder() +var builder = Host.CreateDefaultBuilder() // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. - .UseApplicationMetadata() - .Build() - .StartAsync(); +builder.UseApplicationMetadata(); + +var host = builder.Build(); +await host.StartAsync(); var metadataOptions = host.Services.GetRequiredService>(); var buildVersion = metadataOptions.Value.BuildVersion; @@ -67,7 +94,7 @@ var buildVersion = metadataOptions.Value.BuildVersion; Alternatively, you can achieve the same result as above by doing this: ```cs -var hostBuilder = Host.CreateDefaultBuilder() +var host = Host.CreateDefaultBuilder() .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => configurationBuilder .AddApplicationMetadata(hostBuilderContext.HostingEnvironment)) .ConfigureServices((hostBuilderContext, serviceCollection) => serviceCollection @@ -77,3 +104,143 @@ var hostBuilder = Host.CreateDefaultBuilder() var metadataOptions = host.Services.GetRequiredService>(); var buildVersion = metadataOptions.Value.BuildVersion; ``` + +Your `appsettings.json` can have a section as follows : + +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: + +### Configure with IHostApplicationBuilder + +For applications using : + +```csharp +```cs +var builder = Host.CreateApplicationBuilder() + // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` + // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. +builder.UseApplicationMetadata(); + +var host = builder.Build(); +await host.StartAsync(); + +var metadataOptions = host.Services.GetRequiredService>(); +var buildVersion = metadataOptions.Value.BuildVersion; +``` + +Your `appsettings.json` can have a section as follows : + +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: + +## Access application metadata + +Once configured, you can inject and use the type: + +```csharp +using Microsoft.Extensions.AmbientMetadata; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; + +var builder = Host.CreateApplicationBuilder(args); +builder.UseApplicationMetadata(); + +var host = builder.Build(); + +var _metadata = host.Services.GetRequiredService>().Value; +Console.WriteLine($"Application: {_metadata.ApplicationName}"); +Console.WriteLine($"Version: {_metadata.BuildVersion}"); +Console.WriteLine($"Environment: {_metadata.EnvironmentName}"); +Console.WriteLine($"Deployment Ring: {_metadata.DeploymentRing}"); +await host.RunAsync(); +``` +## ApplicationMetadata properties + +The class includes the following properties: + +| Property | Description | +|----------|-------------| +| `ApplicationName` | The name of the application. | +| `BuildVersion` | The version of the application build. | +| `DeploymentRing` | The deployment ring or stage (for example, Canary, Production). | +| `EnvironmentName` | The environment where the application is running (for example, Development, Staging, Production). | + +## Use with logging + +Application metadata is particularly useful for enriching log messages: + +```csharp +using Microsoft.Extensions.AmbientMetadata; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +var builder = Host.CreateApplicationBuilder(); + +builder.UseApplicationMetadata(); +builder.ConfigureServices(services => +{ + services.AddSingleton(); +}); + +var host = builder.Build(); + +var loggingService = host.Services.GetRequiredService(); +loggingService.LogWithMetadata(); + +await host.RunAsync(); + +public class LoggingService +{ + private readonly ILogger _logger; + private readonly ApplicationMetadata _metadata; + + public LoggingService(ILogger logger, IOptions metadata) + { + _logger = logger; + _metadata = metadata.Value; + } + + public void LogWithMetadata() + { + _logger.LogInformation( + "Processing request in {ApplicationName} v{Version} ({Environment})", + _metadata.ApplicationName, + _metadata.BuildVersion, + _metadata.EnvironmentName); + } +} +``` + +## Custom configuration section + +If you prefer to use a different configuration section name, specify it when calling : + +```csharp +using Microsoft.Extensions.Hosting; + +var builder = Host.CreateApplicationBuilder(args); + +// Use custom configuration section +builder.UseApplicationMetadata("myapp:metadata"); + +var host = builder.Build(); + +await host.RunAsync(); +``` + +With this configuration, your settings would look like: + +```json +{ + "myapp": { + "metadata": { + "ApplicationName": "MyWebApi", // Your ApplicationName will be imported from `IHostEnvironment` + "BuildVersion": "1.0.0", + "DeploymentRing": "Production", + "EnvironmentName": "Production" // Your EnvironmentName will be imported from `IHostEnvironment` + } + } +} +``` diff --git a/docs/core/enrichment/snippets/servicelogenricher/Program.cs b/docs/core/enrichment/snippets/servicelogenricher/Program.cs index 7330d86457c4d..9c7b7d03e929b 100644 --- a/docs/core/enrichment/snippets/servicelogenricher/Program.cs +++ b/docs/core/enrichment/snippets/servicelogenricher/Program.cs @@ -3,23 +3,17 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -var builder = Host.CreateDefaultBuilder(args); +var builder = Host.CreateApplicationBuilder(args); builder.UseApplicationMetadata(); -builder.ConfigureLogging(builder => +builder.Logging.EnableEnrichment(); +builder.Logging.AddJsonConsole(op => { - builder.EnableEnrichment(); - builder.AddJsonConsole(op => + op.JsonWriterOptions = new JsonWriterOptions { - op.JsonWriterOptions = new JsonWriterOptions - { - Indented = true - }; - }); -}); -builder.ConfigureServices((context, services) => -{ - services.AddServiceLogEnricher(context.Configuration.GetSection("ApplicationLogEnricherOptions")); + Indented = true + }; }); +builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions")); var host = builder.Build(); var logger = host.Services.GetRequiredService>(); @@ -27,5 +21,3 @@ logger.LogInformation("This is a sample log message"); await host.RunAsync(); - - diff --git a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj index 7019c1d302f00..714f32824ed44 100644 --- a/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj +++ b/docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj @@ -8,11 +8,11 @@ - - - - - + + + + + From 037045c7a6df273701684c515a8e29d551623bd4 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 15:39:45 +0200 Subject: [PATCH 08/14] missing new line --- docs/core/enrichment/application-metadata.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 51e55a2401fca..5b9e63a0ecd68 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -154,6 +154,7 @@ Console.WriteLine($"Environment: {_metadata.EnvironmentName}"); Console.WriteLine($"Deployment Ring: {_metadata.DeploymentRing}"); await host.RunAsync(); ``` + ## ApplicationMetadata properties The class includes the following properties: From 27be5846a44935acc727a6896d70955985564ec3 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 15:45:54 +0200 Subject: [PATCH 09/14] missing tick --- docs/core/enrichment/application-metadata.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 5b9e63a0ecd68..bf5beae416a8b 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -216,7 +216,7 @@ public class LoggingService ## Custom configuration section -If you prefer to use a different configuration section name, specify it when calling : +If you prefer to use a different configuration section name, specify it when calling : ```csharp using Microsoft.Extensions.Hosting; From a9c04a0bf4478bd1aaeb65112a1ffbc163fe257f Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 15:50:24 +0200 Subject: [PATCH 10/14] fix --- docs/core/enrichment/application-metadata.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index bf5beae416a8b..1481fb4563d98 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -216,7 +216,7 @@ public class LoggingService ## Custom configuration section -If you prefer to use a different configuration section name, specify it when calling : +If you prefer to use a different configuration section name, specify it when calling : ```csharp using Microsoft.Extensions.Hosting; From b7988fc47c9d3aa0545476021aeec5cf08c46e5e Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 15:55:50 +0200 Subject: [PATCH 11/14] fix --- docs/core/enrichment/application-metadata.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 1481fb4563d98..96fb50a2fb595 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -216,7 +216,7 @@ public class LoggingService ## Custom configuration section -If you prefer to use a different configuration section name, specify it when calling : +If you prefer to use a different configuration section name, specify it when calling : ```csharp using Microsoft.Extensions.Hosting; From 9b57cf11232c42879c2a38cb4e79b8159cecb26f Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Thu, 23 Oct 2025 16:02:12 +0200 Subject: [PATCH 12/14] fixing code blokcs --- docs/core/enrichment/application-metadata.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index 96fb50a2fb595..a218f57ae06bf 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -78,7 +78,7 @@ The following table shows the metadata made available by the provider via configurationBuilder .AddApplicationMetadata(hostBuilderContext.HostingEnvironment)) @@ -114,7 +114,6 @@ Your `appsettings.json` can have a section as follows : For applications using : ```csharp -```cs var builder = Host.CreateApplicationBuilder() // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. From 536c766b6d3530f4a037afa4263a3c35e05d41f8 Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Fri, 24 Oct 2025 09:42:47 +0200 Subject: [PATCH 13/14] comments --- .../enrichment/application-log-enricher.md | 13 +++--- docs/core/enrichment/application-metadata.md | 42 +++++++++---------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/docs/core/enrichment/application-log-enricher.md b/docs/core/enrichment/application-log-enricher.md index e67b36333bff4..2bf801420aecf 100644 --- a/docs/core/enrichment/application-log-enricher.md +++ b/docs/core/enrichment/application-log-enricher.md @@ -52,7 +52,7 @@ Follow these steps to configure the application log enricher in your application First, configure the [Application Metadata](application-metadata.md) by calling the methods: ```csharp -var builder = Host.CreateApplicationBuilder(); +var builder = Host.CreateApplicationBuilder(args); builder.UseApplicationMetadata() ``` @@ -61,11 +61,12 @@ This method automatically picks up values from the , which registers a configuration provider for application metadata by picking up the values from the and adds it to the given configuration section name. Then you use method to register the metadata in the dependency injection container, which allow you to pass separately: ```csharp -var hostBuilder = Host.CreateApplicationBuilder() - .ConfigureAppConfiguration((context, builder) => - builder.AddApplicationMetadata(context.HostingEnvironment)) - .ConfigureServices((context, services) => - services.AddApplicationMetadata(context.Configuration.GetSection("ambientmetadata:application"))); +var builder = Host.CreateApplicationBuilder(args) + .ConfigureAppConfiguration(static (context, builder) => + builder.AddApplicationMetadata(context.HostingEnvironment)); + +builder.Services.AddApplicationMetadata( + builder.Configuration.GetSection("ambientmetadata:application"))); ``` #### 2. Provide additional configuration (optional) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index a218f57ae06bf..e99ad7d264f36 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -79,7 +79,7 @@ The following table shows the metadata made available by the provider via configurationBuilder - .AddApplicationMetadata(hostBuilderContext.HostingEnvironment)) - .ConfigureServices((hostBuilderContext, serviceCollection) => serviceCollection - .AddApplicationMetadata(hostBuilderContext.Configuration.GetSection("ambientmetadata:application"))) - .Build(); +var builder = Host.CreateApplicationBuilder() + .ConfigureAppConfiguration(static (context, builder) => + builder.AddApplicationMetadata(context.HostingEnvironment)); +builder.Services.AddApplicationMetadata( + builder.Configuration.GetSection("ambientmetadata:application"))); +var host = builder.Build(); var metadataOptions = host.Services.GetRequiredService>(); var buildVersion = metadataOptions.Value.BuildVersion; @@ -114,7 +114,7 @@ Your `appsettings.json` can have a section as follows : For applications using : ```csharp -var builder = Host.CreateApplicationBuilder() +var builder = Host.CreateApplicationBuilder(args) // ApplicationName and EnvironmentName will be imported from `IHostEnvironment` // BuildVersion and DeploymentRing will be imported from the "appsettings.json" file. builder.UseApplicationMetadata(); @@ -146,11 +146,11 @@ builder.UseApplicationMetadata(); var host = builder.Build(); -var _metadata = host.Services.GetRequiredService>().Value; -Console.WriteLine($"Application: {_metadata.ApplicationName}"); -Console.WriteLine($"Version: {_metadata.BuildVersion}"); -Console.WriteLine($"Environment: {_metadata.EnvironmentName}"); -Console.WriteLine($"Deployment Ring: {_metadata.DeploymentRing}"); +var metadata = host.Services.GetRequiredService>().Value; +Console.WriteLine($"Application: {metadata.ApplicationName}"); +Console.WriteLine($"Version: {metadata.BuildVersion}"); +Console.WriteLine($"Environment: {metadata.EnvironmentName}"); +Console.WriteLine($"Deployment Ring: {metadata.DeploymentRing}"); await host.RunAsync(); ``` @@ -176,7 +176,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -var builder = Host.CreateApplicationBuilder(); +var builder = Host.CreateApplicationBuilder(args); builder.UseApplicationMetadata(); builder.ConfigureServices(services => @@ -191,16 +191,12 @@ loggingService.LogWithMetadata(); await host.RunAsync(); -public class LoggingService +public class LoggingService( + ILogger logger, + IOptions metadata) { - private readonly ILogger _logger; - private readonly ApplicationMetadata _metadata; - - public LoggingService(ILogger logger, IOptions metadata) - { - _logger = logger; - _metadata = metadata.Value; - } + private readonly ILogger _logger = logger; + private readonly ApplicationMetadata _metadata = metadata.Value; public void LogWithMetadata() { From 7e3f4ce63d5c0861453efab3deddabed2959caba Mon Sep 17 00:00:00 2001 From: Mariam Aziz Date: Fri, 24 Oct 2025 09:50:42 +0200 Subject: [PATCH 14/14] fix sample --- docs/core/enrichment/application-metadata.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/core/enrichment/application-metadata.md b/docs/core/enrichment/application-metadata.md index e99ad7d264f36..08e6dcfad3a3c 100644 --- a/docs/core/enrichment/application-metadata.md +++ b/docs/core/enrichment/application-metadata.md @@ -179,10 +179,7 @@ using Microsoft.Extensions.Options; var builder = Host.CreateApplicationBuilder(args); builder.UseApplicationMetadata(); -builder.ConfigureServices(services => -{ - services.AddSingleton(); -}); +builder.Services.AddSingleton(); var host = builder.Build();