Skip to content

Commit 213f71c

Browse files
Merge pull request #49517 from dotnet/main
Merge main into live
2 parents 5223cf5 + eca11f7 commit 213f71c

File tree

21 files changed

+1145
-53
lines changed

21 files changed

+1145
-53
lines changed

.github/ISSUE_TEMPLATE/02-breaking-change.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ body:
2323
options:
2424
- .NET 8
2525
- .NET 9
26-
- .NET 10 Preview 4
27-
- .NET 10 Preview 5
28-
- .NET 10 Preview 6
29-
- .NET 10 Preview 7
3026
- .NET 10 RC 1
3127
- .NET 10 RC 2
3228
- .NET 10 GA
3329
- .NET 11 Preview 1
34-
- .NEt 11 Preview 2
30+
- .NET 11 Preview 2
31+
- .NET 11 Preview 3
32+
- .NET 11 Preview 4
33+
- .NET 11 Preview 5
3534
- Other (please put exact version in description textbox)
3635
validations:
3736
required: true

docs/core/diagnostics/metrics-generator.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ Metric methods are constrained to the following:
8787
- Their names must not start with an underscore.
8888
- Their parameter names must not start with an underscore.
8989
- Their first parameter must be <xref:System.Diagnostics.Metrics.Meter> type.
90-
Metric methods are constrained to the following:
9190

9291
## See also
9392

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: Application log 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 log 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](application-metadata.md) 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+
### [PackageReference](#tab/package-reference)
34+
35+
```xml
36+
<PackageReference Include="Microsoft.Extensions.Telemetry"
37+
Version="*" /> <!-- Adjust version -->
38+
```
39+
40+
---
41+
42+
## Application log enricher
43+
44+
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.
45+
46+
### Step-by-step configuration
47+
48+
Follow these steps to configure the application log enricher in your application:
49+
50+
#### 1. Configure Application Metadata
51+
52+
First, configure the [Application Metadata](application-metadata.md) by calling the <xref:Microsoft.Extensions.Hosting.ApplicationMetadataHostBuilderExtensions.UseApplicationMetadata%2A> methods:
53+
54+
```csharp
55+
var builder = Host.CreateApplicationBuilder(args);
56+
builder.UseApplicationMetadata()
57+
```
58+
59+
This method automatically picks up values from the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> and saves them to the default configuration section `ambientmetadata:application`.
60+
61+
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:
62+
63+
```csharp
64+
var builder = Host.CreateApplicationBuilder(args)
65+
.ConfigureAppConfiguration(static (context, builder) =>
66+
builder.AddApplicationMetadata(context.HostingEnvironment));
67+
68+
builder.Services.AddApplicationMetadata(
69+
builder.Configuration.GetSection("ambientmetadata:application")));
70+
```
71+
72+
#### 2. Provide additional configuration (optional)
73+
74+
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:
75+
76+
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7":::
77+
78+
#### 3. Register the service log enricher
79+
80+
Register the log enricher into the dependency injection container using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:
81+
82+
```csharp
83+
serviceCollection.AddServiceLogEnricher();
84+
```
85+
86+
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})>:
87+
88+
```csharp
89+
serviceCollection.AddServiceLogEnricher(options =>
90+
{
91+
options.BuildVersion = true;
92+
options.DeploymentRing = true;
93+
});
94+
```
95+
96+
Alternatively, configure options using `appsettings.json`:
97+
98+
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="8-11":::
99+
100+
And apply the configuration using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)>:
101+
102+
```csharp
103+
var builder = Host.CreateApplicationBuilder(args);
104+
builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
105+
106+
```
107+
108+
### `ApplicationLogEnricherOptions` Configuration options
109+
110+
The service log enricher supports several configuration options through the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> class:
111+
112+
| Property | Default Value | Dimension Name | Description |
113+
|----------|---------------|----------------|-------------|
114+
| `EnvironmentName` | true | `deployment.environment` | Environment name from hosting environment or configuration |
115+
| `ApplicationName` | true | `service.name` | Application name from hosting environment or configuration |
116+
| `BuildVersion` | false | `service.version` | Build version from configuration |
117+
| `DeploymentRing` | false | `DeploymentRing` | Deployment ring from configuration |
118+
119+
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.
120+
121+
### Complete example
122+
123+
Here's a complete example showing how to set up the service log enricher:
124+
125+
**appsettings.json:**
126+
127+
:::code language="json" source="snippets/servicelogenricher/appsettings.json":::
128+
129+
**Program.cs:**
130+
131+
:::code language="csharp" source="snippets/servicelogenricher/Program.cs" :::
132+
133+
### Enriched log output
134+
135+
With the service log enricher configured, your log output will include service-specific dimensions:
136+
137+
:::code language="json" source="snippets/servicelogenricher/output-full.json" highlight="8-11" :::
138+
139+
## Next steps
140+
141+
- Learn about [application metadata configuration](application-metadata.md)

0 commit comments

Comments
 (0)