|
| 1 | +--- |
| 2 | +title: Application enricher |
| 3 | +description: Learn how to use the application log enricher to add application-specific information to your telemetry in .NET. |
| 4 | +ms.date: 10/14/2025 |
| 5 | +--- |
| 6 | + |
| 7 | +# Application enricher |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +## Install the package |
| 18 | + |
| 19 | +To get started, install the [📦 Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) NuGet package: |
| 20 | + |
| 21 | +### [.NET CLI](#tab/dotnet-cli) |
| 22 | + |
| 23 | +```dotnetcli |
| 24 | +dotnet add package Microsoft.Extensions.Telemetry |
| 25 | +``` |
| 26 | + |
| 27 | +Or, if you're using .NET 10+ SDK: |
| 28 | + |
| 29 | +```dotnetcli |
| 30 | +dotnet package add Microsoft.Extensions.Telemetry |
| 31 | +``` |
| 32 | + |
| 33 | +## Application log enricher |
| 34 | + |
| 35 | +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. |
| 36 | + |
| 37 | +### Step-by-step configuration |
| 38 | + |
| 39 | +Follow these steps to configure the application log enricher in your application: |
| 40 | + |
| 41 | +#### 1. Configure Application Metadata |
| 42 | + |
| 43 | +First, configure the [Application Metadata](xref:application-metadata) by calling the <xref:Microsoft.Extensions.Hosting.ApplicationMetadataHostBuilderExtensions.UseApplicationMetadata> method: |
| 44 | + |
| 45 | +```csharp |
| 46 | +var builder = Host.CreateDefaultBuilder(); |
| 47 | +builder.UseApplicationMetadata() |
| 48 | +``` |
| 49 | + |
| 50 | +This method automatically picks up values from the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> and saves them to the default configuration section `ambientmetadata:application`. |
| 51 | + |
| 52 | +Alternatively, you can use this method <xref:Microsoft.Extensions.Configuration.ApplicationMetadataConfigurationBuilderExtensions.AddApplicationMetadata(Microsoft.Extensions.Configuration.IConfigurationBuilder,Microsoft.Extensions.Hosting.IHostEnvironment,System.String)>, which registers a configuration provider for application metadata by picking up the values from the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> and adds it to the given configuration section name. Then you use <xref:Microsoft.Extensions.DependencyInjection.ApplicationMetadataServiceCollectionExtensions.AddApplicationMetadata(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)> method to register the metadata in the dependency injection container, which allow you to pass <xref:Microsoft.Extensions.Configuration.IConfigurationSection> separately: |
| 53 | + |
| 54 | +```csharp |
| 55 | +var hostBuilder = Host.CreateDefaultBuilder() |
| 56 | + .ConfigureAppConfiguration((context, builder) => |
| 57 | + builder.AddApplicationMetadata(context.HostingEnvironment)) |
| 58 | + .ConfigureServices((context, services) => |
| 59 | + services.AddApplicationMetadata(context.Configuration.GetSection("ambientmetadata:application"))); |
| 60 | +``` |
| 61 | + |
| 62 | +#### 2. Provide additional configuration (optional) |
| 63 | + |
| 64 | +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: |
| 65 | + |
| 66 | +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7"::: |
| 67 | + |
| 68 | +#### 3. Register the service log enricher |
| 69 | + |
| 70 | +Register the log enricher into the dependency injection container using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher>: |
| 71 | + |
| 72 | +```csharp |
| 73 | +serviceCollection.AddServiceLogEnricher(); |
| 74 | +``` |
| 75 | + |
| 76 | +You can enable or disable individual options of the enricher using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action(Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions))>: |
| 77 | + |
| 78 | +```csharp |
| 79 | +serviceCollection.AddServiceLogEnricher(options => |
| 80 | +{ |
| 81 | + options.BuildVersion = true; |
| 82 | + options.DeploymentRing = true; |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +Alternatively, configure options using `appsettings.json`: |
| 87 | + |
| 88 | +:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="9-12"::: |
| 89 | + |
| 90 | +And apply the configuration using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)>: |
| 91 | + |
| 92 | +```csharp |
| 93 | +var builder = Host.CreateDefaultBuilder(args); |
| 94 | +builder.ConfigureServices((context, services) => |
| 95 | +{ |
| 96 | + services.AddServiceLogEnricher(context.Configuration.GetSection("applicationlogenricheroptions")); |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +### `ApplicationLogEnricherOptions` Configuration options |
| 101 | + |
| 102 | +The service log enricher supports several configuration options through the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> class: |
| 103 | + |
| 104 | +| Property | Default Value | Dimension Name | Description | |
| 105 | +|----------|---------------|----------------|-------------| |
| 106 | +| `EnvironmentName` | true | `deployment.environment` | Environment name from hosting environment or configuration | |
| 107 | +| `ApplicationName` | true | `service.name` | Application name from hosting environment or configuration | |
| 108 | +| `BuildVersion` | false | `service.version` | Build version from configuration | |
| 109 | +| `DeploymentRing` | false | `DeploymentRing` | Deployment ring from configuration | |
| 110 | + |
| 111 | +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. |
| 112 | + |
| 113 | +### Complete example |
| 114 | + |
| 115 | +Here's a complete example showing how to set up the service log enricher: |
| 116 | + |
| 117 | +**appsettings.json:** |
| 118 | + |
| 119 | +:::code language="json" source="snippets/servicelogenricher/appsettings.json"::: |
| 120 | + |
| 121 | +**Program.cs:** |
| 122 | + |
| 123 | +:::code language="csharp" source="snippets/servicelogenricher/Program.cs" ::: |
| 124 | + |
| 125 | +### Enriched log output |
| 126 | + |
| 127 | +With the service log enricher configured, your log output will include service-specific dimensions: |
| 128 | + |
| 129 | +:::code language="csharp" source="snippets/servicelogenricher/output-full.json" highlight="9-11" ::: |
| 130 | + |
| 131 | +## Next steps |
| 132 | + |
| 133 | +- Learn about [application metadata configuration](xref:application-metadata) |
0 commit comments