Skip to content

Commit 25cf86b

Browse files
authored
Add EXTOBS0002 diagnostic (#49873)
1 parent 134d825 commit 25cf86b

File tree

7 files changed

+100
-114
lines changed

7 files changed

+100
-114
lines changed

docs/core/enrichment/application-log-enricher.md

Lines changed: 26 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Application log enricher
33
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
4+
ms.date: 11/12/2025
55
---
66

77
# Application log enricher
@@ -39,15 +39,11 @@ dotnet package add Microsoft.Extensions.Telemetry
3939

4040
---
4141

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
42+
## Step-by-step configuration
4743

4844
Follow these steps to configure the application log enricher in your application:
4945

50-
#### 1. Configure Application Metadata
46+
### 1. Configure application metadata
5147

5248
First, configure the [Application Metadata](application-metadata.md) by calling the <xref:Microsoft.Extensions.Hosting.ApplicationMetadataHostBuilderExtensions.UseApplicationMetadata%2A> methods:
5349

@@ -58,7 +54,7 @@ builder.UseApplicationMetadata()
5854

5955
This method automatically picks up values from the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> and saves them to the default configuration section `ambientmetadata:application`.
6056

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:
57+
Alternatively, you can use the <xref:Microsoft.Extensions.Configuration.ApplicationMetadataConfigurationBuilderExtensions.AddApplicationMetadata(Microsoft.Extensions.Configuration.IConfigurationBuilder,Microsoft.Extensions.Hosting.IHostEnvironment,System.String)> method, which registers a configuration provider for application metadata by picking up the values from the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> and adding them to the given configuration section name. Then you use the <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 allows you to pass <xref:Microsoft.Extensions.Configuration.IConfigurationSection> separately:
6258

6359
```csharp
6460
var builder = Host.CreateApplicationBuilder(args)
@@ -69,19 +65,15 @@ builder.Services.AddApplicationMetadata(
6965
builder.Configuration.GetSection("ambientmetadata:application")));
7066
```
7167

72-
#### 2. Provide additional configuration (optional)
68+
### 2. Provide additional configuration (optional)
7369

7470
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:
7571

76-
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7":::
77-
78-
#### 3. Register the service log enricher
72+
:::code language="json" source="snippets/applicationlogenricher/appsettings.json" range="2-7":::
7973

80-
Register the log enricher into the dependency injection container:
74+
### 3. Register the application log enricher
8175

82-
### [.NET 10.1+](#tab/net10-plus)
83-
84-
Starting with .NET 10, use the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method:
76+
Register the log enricher into the dependency injection container by calling the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method:
8577

8678
```csharp
8779
serviceCollection.AddApplicationLogEnricher();
@@ -97,113 +89,50 @@ serviceCollection.AddApplicationLogEnricher(options =>
9789
});
9890
```
9991

100-
### [.NET 9 and earlier](#tab/net9-earlier)
101-
102-
For .NET 9 and earlier versions, use the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method:
103-
104-
> [!WARNING]
105-
> The `AddServiceLogEnricher` method is obsolete starting with .NET 10.1. Use `AddApplicationLogEnricher` instead.
106-
107-
```csharp
108-
serviceCollection.AddServiceLogEnricher();
109-
```
110-
111-
You can enable or disable individual options of the enricher:
112-
113-
```csharp
114-
serviceCollection.AddServiceLogEnricher(options =>
115-
{
116-
options.BuildVersion = true;
117-
options.DeploymentRing = true;
118-
});
119-
```
120-
121-
---
92+
> [!NOTE]
93+
> If you're using .NET 9 or an earlier version, call the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method instead.
12294
12395
Alternatively, configure options using `appsettings.json`:
12496

125-
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="8-11":::
97+
:::code language="json" source="snippets/applicationlogenricher/appsettings.json" range="8-11":::
12698

127-
And apply the configuration:
128-
129-
### [.NET 10.1+](#tab/net10-plus-config)
99+
Next, apply the configuration.
130100

131101
```csharp
132102
var builder = Host.CreateApplicationBuilder(args);
133103
builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
134104
```
135105

136-
### [.NET 9 and earlier](#tab/net9-earlier-config)
106+
### `ApplicationLogEnricherOptions` configuration options
137107

138-
```csharp
139-
var builder = Host.CreateApplicationBuilder(args);
140-
builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
141-
```
142-
143-
---
144-
145-
### `ApplicationLogEnricherOptions` Configuration options
146-
147-
The service log enricher supports several configuration options through the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> class:
108+
The application log enricher supports several configuration options through the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> class:
148109

149-
| Property | Default Value | Dimension Name | Description |
150-
|----------|---------------|----------------|-------------|
151-
| `EnvironmentName` | true | `deployment.environment` | Environment name from hosting environment or configuration |
152-
| `ApplicationName` | true | `service.name` | Application name from hosting environment or configuration |
153-
| `BuildVersion` | false | `service.version` | Build version from configuration |
154-
| `DeploymentRing` | false | `DeploymentRing` | Deployment ring from configuration |
110+
| Property | Default value | Dimension name | Description |
111+
|-------------------|---------------|--------------------------|------------------------------------------------------------|
112+
| `EnvironmentName` | true | `deployment.environment` | Environment name from hosting environment or configuration |
113+
| `ApplicationName` | true | `service.name` | Application name from hosting environment or configuration |
114+
| `BuildVersion` | false | `service.version` | Build version from configuration |
115+
| `DeploymentRing` | false | `DeploymentRing` | Deployment ring from configuration |
155116

156117
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.
157118

158-
### Complete example
119+
## Complete example
159120

160-
Here's a complete example showing how to set up the service log enricher:
121+
Here's a complete example showing how to set up the application log enricher:
161122

162123
**appsettings.json:**
163124

164-
:::code language="json" source="snippets/servicelogenricher/appsettings.json":::
125+
:::code language="json" source="snippets/applicationlogenricher/appsettings.json":::
165126

166127
**Program.cs:**
167128

168-
### [.NET 10.1+](#tab/net10-plus-full-example)
169-
170-
```csharp
171-
using System.Text.Json;
172-
using Microsoft.Extensions.DependencyInjection;
173-
using Microsoft.Extensions.Hosting;
174-
using Microsoft.Extensions.Logging;
175-
176-
var builder = Host.CreateApplicationBuilder(args);
177-
builder.UseApplicationMetadata();
178-
builder.Logging.EnableEnrichment();
179-
builder.Logging.AddJsonConsole(op =>
180-
{
181-
op.JsonWriterOptions = new JsonWriterOptions
182-
{
183-
Indented = true
184-
};
185-
});
186-
builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
187-
188-
var host = builder.Build();
189-
var logger = host.Services.GetRequiredService<ILogger<Program>>();
190-
191-
logger.LogInformation("This is a sample log message");
192-
193-
await host.RunAsync();
194-
```
195-
196-
### [.NET 9 and earlier](#tab/net9-earlier-full-example)
197-
198-
:::code language="csharp" source="snippets/servicelogenricher/Program.cs" :::
199-
200-
---
129+
:::code language="csharp" source="snippets/applicationlogenricher/Program.cs" :::
201130

202131
### Enriched log output
203132

204-
With the service log enricher configured, your log output will include service-specific dimensions:
133+
With the application log enricher configured, your log output includes service-specific dimensions:
205134

206-
:::code language="json" source="snippets/servicelogenricher/output-full.json" highlight="8-11" :::
135+
:::code language="json" source="snippets/applicationlogenricher/output-full.json" highlight="8-11" :::
207136

208137
## Next steps
209138

docs/core/enrichment/application-metadata.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ Optionally, you can provide values for `BuildVersion` and `DeploymentRing` via t
7171

7272
The following table shows the metadata made available by the provider via <xref:Microsoft.Extensions.Configuration.IConfiguration>:
7373

74-
| Key | Required? | Where the value comes from| Value Example | Description|
75-
|-|-|-|-|-|
74+
| Key | Required? | Where the value comes from | Value example | Description |
75+
|-----|-----------|----------------------------|---------------|-------------|
7676
| `ambientmetadata:application:applicationname` | yes | automatically from `IHostEnvironment` |`myApp` | The application name.|
7777
| `ambientmetadata:application:environmentname` | yes | automatically from `IHostEnvironment` | `Production`, `Development`| The environment the application is deployed to.|
7878
| `ambientmetadata:application:buildversion` | no | configure it in `IConfiguration` | `1.0.0-rc1` | The application's build version.|
7979
| `ambientmetadata:application:deploymentring` | no | configure it in `IConfiguration` | `r0`, `public` | The deployment ring from where the application is running.|
8080

8181
```csharp
8282
var builder = Host.CreateDefaultBuilder(args)
83-
// ApplicationName and EnvironmentName will be imported from `IHostEnvironment`
83+
// ApplicationName and EnvironmentName will be imported from `IHostEnvironment`.
8484
// BuildVersion and DeploymentRing will be imported from the "appsettings.json" file.
8585
builder.UseApplicationMetadata();
8686

@@ -105,17 +105,17 @@ var metadataOptions = host.Services.GetRequiredService<IOptions<ApplicationMetad
105105
var buildVersion = metadataOptions.Value.BuildVersion;
106106
```
107107

108-
Your `appsettings.json` can have a section as follows :
108+
Your `appsettings.json` can have a section as follows:
109109

110-
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7":::
110+
:::code language="json" source="snippets/applicationlogenricher/appsettings.json" range="2-7":::
111111

112112
### Configure with IHostApplicationBuilder
113113

114114
For applications using <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder>:
115115

116116
```csharp
117117
var builder = Host.CreateApplicationBuilder(args)
118-
// ApplicationName and EnvironmentName will be imported from `IHostEnvironment`
118+
// ApplicationName and EnvironmentName will be imported from `IHostEnvironment`.
119119
// BuildVersion and DeploymentRing will be imported from the "appsettings.json" file.
120120
builder.UseApplicationMetadata();
121121

@@ -126,9 +126,9 @@ var metadataOptions = host.Services.GetRequiredService<IOptions<ApplicationMetad
126126
var buildVersion = metadataOptions.Value.BuildVersion;
127127
```
128128

129-
Your `appsettings.json` can have a section as follows :
129+
Your `appsettings.json` can have a section as follows:
130130

131-
:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="2-7":::
131+
:::code language="json" source="snippets/applicationlogenricher/appsettings.json" range="2-7":::
132132

133133
## Access application metadata
134134

@@ -158,11 +158,11 @@ await host.RunAsync();
158158

159159
The <xref:Microsoft.Extensions.AmbientMetadata.ApplicationMetadata> class includes the following properties:
160160

161-
| Property | Description |
162-
|----------|-------------|
163-
| `ApplicationName` | The name of the application. |
164-
| `BuildVersion` | The version of the application build. |
165-
| `DeploymentRing` | The deployment ring or stage (for example, Canary, Production). |
161+
| Property | Description |
162+
|-------------------|-----------------------------------------------------------------|
163+
| `ApplicationName` | The name of the application. |
164+
| `BuildVersion` | The version of the application build. |
165+
| `DeploymentRing` | The deployment ring or stage (for example, Canary, Production). |
166166
| `EnvironmentName` | The environment where the application is running (for example, Development, Staging, Production). |
167167

168168
## Use with logging
@@ -229,10 +229,10 @@ With this configuration, your settings would look like:
229229
{
230230
"myapp": {
231231
"metadata": {
232-
"ApplicationName": "MyWebApi", // Your ApplicationName will be imported from `IHostEnvironment`
232+
"ApplicationName": "MyWebApi", // ApplicationName will be imported from `IHostEnvironment`.
233233
"BuildVersion": "1.0.0",
234234
"DeploymentRing": "Production",
235-
"EnvironmentName": "Production" // Your EnvironmentName will be imported from `IHostEnvironment`
235+
"EnvironmentName": "Production" // EnvironmentName will be imported from `IHostEnvironment`.
236236
}
237237
}
238238
}

docs/core/enrichment/snippets/servicelogenricher/Program.cs renamed to docs/core/enrichment/snippets/applicationlogenricher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Indented = true
1414
};
1515
});
16-
builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
16+
builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
1717

1818
var host = builder.Build();
1919
var logger = host.Services.GetRequiredService<ILogger<Program>>();

docs/core/enrichment/snippets/servicelogenricher/servicelogenricher.csproj renamed to docs/core/enrichment/snippets/applicationlogenricher/applogenricher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

docs/core/enrichment/snippets/servicelogenricher/appsettings.json renamed to docs/core/enrichment/snippets/applicationlogenricher/appsettings.json

File renamed without changes.

docs/core/enrichment/snippets/servicelogenricher/output-full.json renamed to docs/core/enrichment/snippets/applicationlogenricher/output-full.json

File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: EXTOBS0002 warning
3+
description: Learn about the obsoletions that generate compile-time warning EXTOBS0002.
4+
ms.date: 12/22/2025
5+
f1_keywords:
6+
- extobs0002
7+
ai-usage: ai-assisted
8+
---
9+
# EXTOBS0002: AddServiceLogEnricher is obsolete
10+
11+
The `AddServiceLogEnricher` extension methods have been marked as obsolete starting in package version 10.1.0. These methods had incorrect naming that didn't accurately reflect their functionality. The methods enrich application logs, not service logs, so they have been replaced with correctly named `AddApplicationLogEnricher` methods.
12+
13+
The following APIs are marked obsolete. Use of these APIs generates warning `EXTOBS0002` at compile time.
14+
15+
- <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)?displayProperty=nameWithType>
16+
- <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)?displayProperty=nameWithType>
17+
- <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions})?displayProperty=nameWithType>
18+
19+
## Workarounds
20+
21+
Replace calls to `AddServiceLogEnricher` with the equivalent `AddApplicationLogEnricher` methods. The functionality remains the same, only the method names have been corrected to accurately reflect that they enrich application logs.
22+
23+
For more information, see [Application log enricher](../../core/enrichment/application-log-enricher.md).
24+
25+
## Suppress a warning
26+
27+
If you must use the obsolete APIs, you can suppress the warning in code or in your project file.
28+
29+
To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.
30+
31+
```csharp
32+
// Disable the warning.
33+
#pragma warning disable EXTOBS0002
34+
35+
// Code that uses obsolete API.
36+
// ...
37+
38+
// Re-enable the warning.
39+
#pragma warning restore EXTOBS0002
40+
```
41+
42+
To suppress all the `EXTOBS0002` warnings in your project, add a `<NoWarn>` property to your project file.
43+
44+
```xml
45+
<Project Sdk="Microsoft.NET.Sdk">
46+
<PropertyGroup>
47+
...
48+
<NoWarn>$(NoWarn);EXTOBS0002</NoWarn>
49+
</PropertyGroup>
50+
</Project>
51+
```
52+
53+
For more information, see [Suppress warnings](obsoletions-overview.md#suppress-warnings).
54+
55+
## See also
56+
57+
- [Application log enricher](../../core/enrichment/application-log-enricher.md)

0 commit comments

Comments
 (0)