Skip to content

Commit 9362240

Browse files
Deprecate AddServiceLogEnricher method with its overloads, introduce replacements (#6529)
1 parent 52d4321 commit 9362240

12 files changed

Lines changed: 199 additions & 39 deletions

docs/list-of-diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ You may continue using obsolete APIs in your application, but we advise explorin
5151
| Diagnostic ID | Description |
5252
| :---------------- | :---------- |
5353
| `EXTOBS0001` | This API is obsolete and will be removed in a future version. Consider using [Resource Monitoring observable instruments](https://learn.microsoft.com/dotnet/core/diagnostics/built-in-metrics-diagnostics#microsoftextensionsdiagnosticsresourcemonitoring). |
54+
| `EXTOBS0002` | This API is obsolete and will be removed in a future version. Instead of the AddServiceLogEnricher() methods, consider using the respective AddApplicationLogEnricher() methods. |
5455

5556
# LoggerMessage
5657

src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationEnricherServiceCollectionExtensions.cs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.Diagnostics.Enrichment;
7+
using Microsoft.Shared.DiagnosticIds;
78
using Microsoft.Shared.Diagnostics;
89

910
namespace Microsoft.Extensions.DependencyInjection;
@@ -19,22 +20,63 @@ public static class ApplicationEnricherServiceCollectionExtensions
1920
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
2021
/// <returns>The value of <paramref name="services"/>.</returns>
2122
/// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null"/>.</exception>
22-
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services)
23+
[Obsolete(
24+
DiagnosticIds.Obsoletions.ObsoleteTelemetryApiMessage,
25+
DiagnosticId = DiagnosticIds.Obsoletions.ObsoleteTelemetryApiDiagId,
26+
UrlFormat = DiagnosticIds.UrlFormat)]
27+
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services) =>
28+
services.AddApplicationLogEnricher(_ => { });
29+
30+
/// <summary>
31+
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
32+
/// </summary>
33+
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
34+
/// <param name="configure">The <see cref="ApplicationLogEnricherOptions"/> configuration delegate.</param>
35+
/// <returns>The value of <paramref name="services"/>.</returns>
36+
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
37+
[Obsolete(
38+
DiagnosticIds.Obsoletions.ObsoleteTelemetryApiMessage,
39+
DiagnosticId = DiagnosticIds.Obsoletions.ObsoleteTelemetryApiDiagId,
40+
UrlFormat = DiagnosticIds.UrlFormat)]
41+
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure) =>
42+
services.AddApplicationLogEnricher(configure);
43+
44+
/// <summary>
45+
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
46+
/// </summary>
47+
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
48+
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ApplicationLogEnricherOptions"/> in the service enricher.</param>
49+
/// <returns>The value of <paramref name="services"/>.</returns>
50+
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
51+
[Obsolete(
52+
DiagnosticIds.Obsoletions.ObsoleteTelemetryApiMessage,
53+
DiagnosticId = DiagnosticIds.Obsoletions.ObsoleteTelemetryApiDiagId,
54+
UrlFormat = DiagnosticIds.UrlFormat)]
55+
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, IConfigurationSection section) =>
56+
services.AddApplicationLogEnricher(section);
57+
58+
/// <summary>
59+
/// Adds an instance of the application enricher to the <see cref="IServiceCollection"/>.
60+
/// </summary>
61+
/// <param name="services">The <see cref="IServiceCollection"/> to add the application enricher to.</param>
62+
/// <returns>The value of <paramref name="services"/>.</returns>
63+
/// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null"/>.</exception>
64+
public static IServiceCollection AddApplicationLogEnricher(this IServiceCollection services)
2365
{
2466
_ = Throw.IfNull(services);
2567

2668
return services
27-
.AddServiceLogEnricher(_ => { });
69+
.AddApplicationLogEnricher(_ => { });
2870
}
2971

3072
/// <summary>
31-
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
73+
/// Adds an instance of the application enricher to the <see cref="IServiceCollection"/>.
3274
/// </summary>
33-
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
75+
/// <param name="services">The <see cref="IServiceCollection"/> to add the application enricher to.</param>
3476
/// <param name="configure">The <see cref="ApplicationLogEnricherOptions"/> configuration delegate.</param>
3577
/// <returns>The value of <paramref name="services"/>.</returns>
3678
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
37-
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure)
79+
public static IServiceCollection AddApplicationLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure)
3880
{
3981
_ = Throw.IfNull(services);
4082
_ = Throw.IfNull(configure);
@@ -45,13 +87,13 @@ public static IServiceCollection AddServiceLogEnricher(this IServiceCollection s
4587
}
4688

4789
/// <summary>
48-
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
90+
/// Adds an instance of the application enricher to the <see cref="IServiceCollection"/>.
4991
/// </summary>
50-
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
51-
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ApplicationLogEnricherOptions"/> in the service enricher.</param>
92+
/// <param name="services">The <see cref="IServiceCollection"/> to add the application enricher to.</param>
93+
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ApplicationLogEnricherOptions"/> in the application enricher.</param>
5294
/// <returns>The value of <paramref name="services"/>.</returns>
5395
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
54-
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, IConfigurationSection section)
96+
public static IServiceCollection AddApplicationLogEnricher(this IServiceCollection services, IConfigurationSection section)
5597
{
5698
_ = Throw.IfNull(services);
5799
_ = Throw.IfNull(section);

src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationLogEnricherOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Microsoft.Extensions.Diagnostics.Enrichment;
77

88
/// <summary>
9-
/// Options for the service log enricher.
9+
/// Options for the application log enricher.
1010
/// </summary>
1111
public class ApplicationLogEnricherOptions
1212
{

src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<Workstream>Telemetry</Workstream>
77
<!-- disable "Published symbols cannot be deleted to maintain compatibility" because we have different APIs for different TFMs -->
88
<NoWarn Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">$(NoWarn);LA0006</NoWarn>
9+
<!-- disable "CS0436: Type conflicts with imported type" for the ObsoleteAttribute class in netstandard2.0 and net462" -->
10+
<NoWarn Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">$(NoWarn);CS0436</NoWarn>
911
</PropertyGroup>
1012

1113
<PropertyGroup>
@@ -20,6 +22,7 @@
2022
<InjectSharedNumericExtensions>true</InjectSharedNumericExtensions>
2123
<InjectSharedPools>true</InjectSharedPools>
2224
<InjectSharedRentedSpan>true</InjectSharedRentedSpan>
25+
<InjectObsoleteAttributeOnLegacy>true</InjectObsoleteAttributeOnLegacy>
2326
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
2427
<UseLoggingGenerator>true</UseLoggingGenerator>
2528
</PropertyGroup>

src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
{
2-
"Name": "Microsoft.Extensions.Telemetry, Version=9.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
2+
"Name": "Microsoft.Extensions.Telemetry, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
33
"Types": [
44
{
55
"Type": "static class Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions",
66
"Stage": "Stable",
77
"Methods": [
88
{
9-
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
9+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
1010
"Stage": "Stable"
1111
},
1212
{
13-
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> configure);",
13+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> configure);",
1414
"Stage": "Stable"
1515
},
1616
{
17-
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfigurationSection section);",
17+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfigurationSection section);",
1818
"Stage": "Stable"
19+
},
20+
{
21+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
22+
"Stage": "Obsolete"
23+
},
24+
{
25+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> configure);",
26+
"Stage": "Obsolete"
27+
},
28+
{
29+
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfigurationSection section);",
30+
"Stage": "Obsolete"
1931
}
2032
]
2133
},

src/Libraries/Microsoft.Extensions.Telemetry/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ revisited in future. Namely, this library uses `Microsoft.Extensions.Logging.Abs
126126
- `Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.ManagedThreadId`
127127
- `Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.MessageTemplate`
128128

129-
### Service Log Enrichment
129+
### Application Log Enrichment
130130

131-
Enriches logs with application-specific information based on `ApplicationMetadata` information. The bellow calls will add the service log enricher to the service collection.
131+
Enriches logs with application-specific information based on `ApplicationMetadata` information. The bellow calls will add the application log enricher to the service collection.
132132

133133
```csharp
134134
// Add service log enricher with default settings
135-
builder.Services.AddServiceLogEnricher();
135+
builder.Services.AddApplicationLogEnricher();
136136

137137
// Or configure with options
138-
builder.Services.AddServiceLogEnricher(options =>
138+
builder.Services.AddApplicationLogEnricher(options =>
139139
{
140140
options.ApplicationName = true;
141141
options.BuildVersion = true;
@@ -197,7 +197,7 @@ builder.Logging.EnableEnrichment(options =>
197197
options.UseFileInfoForStackTraces = true;
198198
});
199199

200-
builder.Services.AddServiceLogEnricher(); // <- This call is required in order for the enricher to be added into the service collection.
200+
builder.Services.AddApplicationLogEnricher(); // <- This call is required in order for the enricher to be added into the service collection.
201201
202202
// Enable log redaction
203203
builder.Logging.EnableRedaction(options =>

src/Shared/DiagnosticIds/DiagnosticIds.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ internal static class AuditReports
123123
internal static class Obsoletions
124124
{
125125
internal const string NonObservableResourceMonitoringApiDiagId = "EXTOBS0001";
126-
internal const string NonObservableResourceMonitoringApiMessage = "This API is obsolete and will be removed in a future version. Consider using Resource Monitoring observable instruments.";
126+
internal const string ObsoleteTelemetryApiDiagId = "EXTOBS0002";
127+
internal const string NonObservableResourceMonitoringApiMessage =
128+
"This API is obsolete and will be removed in a future version. Consider using Resource Monitoring observable instruments.";
129+
internal const string ObsoleteTelemetryApiMessage =
130+
"This API is obsolete and will be removed in a future version. Instead of the AddServiceLogEnricher() methods, consider using the respective AddApplicationLogEnricher() methods.";
127131
}
128132
}
129133

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Enrichment/ApplicationEnricherExtensionsTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ public class ApplicationEnricherExtensionsTests
1818
public void ServiceLogEnricher_GivenAnyNullArgument_Throws()
1919
{
2020
Assert.Throws<ArgumentNullException>(() =>
21-
((IServiceCollection)null!).AddServiceLogEnricher());
21+
((IServiceCollection)null!).AddApplicationLogEnricher());
2222

2323
Assert.Throws<ArgumentNullException>(() =>
24-
((IServiceCollection)null!).AddServiceLogEnricher(_ => { }));
24+
((IServiceCollection)null!).AddApplicationLogEnricher(_ => { }));
2525

2626
Assert.Throws<ArgumentNullException>(() =>
27-
((IServiceCollection)null!).AddServiceLogEnricher(Mock.Of<IConfigurationSection>()));
27+
((IServiceCollection)null!).AddApplicationLogEnricher(Mock.Of<IConfigurationSection>()));
2828

2929
Assert.Throws<ArgumentNullException>(() =>
30-
new ServiceCollection().AddServiceLogEnricher((IConfigurationSection)null!));
30+
new ServiceCollection().AddApplicationLogEnricher((IConfigurationSection)null!));
3131
}
3232

3333
[Fact]
3434
public void ServiceLogEnricher_GivenNoArguments_RegistersInDI()
3535
{
3636
// Arrange & Act
3737
using var host = FakeHost.CreateBuilder()
38-
.ConfigureServices(services => services.AddServiceLogEnricher())
38+
.ConfigureServices(services => services.AddApplicationLogEnricher())
3939
.Build();
4040

4141
// Assert
@@ -48,7 +48,7 @@ public void HostLogEnricher_GivenOptions_RegistersInDI()
4848
// Arrange & Act
4949
using var host = FakeHost.CreateBuilder()
5050
.ConfigureLogging(builder => builder
51-
.Services.AddServiceLogEnricher(e =>
51+
.Services.AddApplicationLogEnricher(e =>
5252
{
5353
e.ApplicationName = false;
5454
e.EnvironmentName = false;
@@ -68,17 +68,17 @@ public void HostLogEnricher_GivenOptions_RegistersInDI()
6868
}
6969

7070
[Fact]
71-
public void ServiceLogEnricher_GivenConfiguration_RegistersInDI()
71+
public void ApplicationLogEnricher_GivenConfiguration_RegistersInDI()
7272
{
7373
// Arrange & Act
7474
using var host = FakeHost.CreateBuilder()
7575
.ConfigureAppConfiguration(
76-
("Serviceenrichersection:ApplicationName", "true"),
77-
("Serviceenrichersection:EnvironmentName", "false"),
78-
("Serviceenrichersection:BuildVersion", "true"),
79-
("Serviceenrichersection:DeploymentRing", "true"))
76+
("Applicationenrichersection:ApplicationName", "true"),
77+
("Applicationenrichersection:EnvironmentName", "false"),
78+
("Applicationenrichersection:BuildVersion", "true"),
79+
("Applicationenrichersection:DeploymentRing", "true"))
8080
.ConfigureServices((context, services) => services
81-
.AddServiceLogEnricher(context.Configuration.GetSection("Serviceenrichersection")))
81+
.AddApplicationLogEnricher(context.Configuration.GetSection("Applicationenrichersection")))
8282
.Build();
8383

8484
// Assert

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Enrichment/ApplicationEnricherOptionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.Extensions.Diagnostics.Enrichment.Test;
99
public class ApplicationEnricherOptionsTests
1010
{
1111
[Fact]
12-
public void ServiceLogEnricherOptions_EnsureDefaultValues()
12+
public void ApplicationLogEnricherOptions_EnsureDefaultValues()
1313
{
1414
var options = new ApplicationLogEnricherOptions();
1515
options.EnvironmentName.Should().BeTrue();

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Enrichment/ApplicationLogEnricherTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ public void HostLogEnricher_GivenInvalidArguments_Throws()
3939
var optionsNull = new Mock<IOptions<ApplicationLogEnricherOptions>>();
4040
optionsNull.Setup(o => o.Value).Returns<IOptions<ApplicationLogEnricherOptions>>(null!);
4141

42-
var serviceOptionsNull = new Mock<IOptions<ApplicationMetadata>>();
43-
serviceOptionsNull.Setup(o => o.Value).Returns<IOptions<ApplicationMetadata>>(null!);
42+
var applicationOptionsNull = new Mock<IOptions<ApplicationMetadata>>();
43+
applicationOptionsNull.Setup(o => o.Value).Returns<IOptions<ApplicationMetadata>>(null!);
4444

4545
// Act & Assert
4646
Assert.Throws<ArgumentException>(() => new ApplicationLogEnricher(optionsNull.Object, null!));
47-
Assert.Throws<ArgumentException>(() => new ApplicationLogEnricher(options, serviceOptionsNull.Object));
47+
Assert.Throws<ArgumentException>(() => new ApplicationLogEnricher(options, applicationOptionsNull.Object));
4848
}
4949

5050
[Theory]
5151
[InlineData(true, true, true, true, null, null)]
5252
[InlineData(true, true, true, true, BuildVersion, DeploymentRing)]
5353
[InlineData(false, false, false, false, null, null)]
5454
[InlineData(false, false, false, false, BuildVersion, DeploymentRing)]
55-
public void ServiceLogEnricher_Options(bool appName, bool envName, bool buildVer, bool depRing, string? buildVersion, string? deploymentRing)
55+
public void ApplicationLogEnricher_Options(bool appName, bool envName, bool buildVer, bool depRing, string? buildVersion, string? deploymentRing)
5656
{
5757
// Arrange
5858
var options = new ApplicationLogEnricherOptions
@@ -63,15 +63,15 @@ public void ServiceLogEnricher_Options(bool appName, bool envName, bool buildVer
6363
DeploymentRing = depRing,
6464
};
6565

66-
var serviceOptions = new ApplicationMetadata
66+
var metadata = new ApplicationMetadata
6767
{
6868
BuildVersion = buildVersion,
6969
DeploymentRing = deploymentRing,
7070
ApplicationName = _hostMock.Object.ApplicationName,
7171
EnvironmentName = _hostMock.Object.EnvironmentName
7272
};
7373

74-
var enricher = new ApplicationLogEnricher(options.ToOptions(), serviceOptions.ToOptions());
74+
var enricher = new ApplicationLogEnricher(options.ToOptions(), metadata.ToOptions());
7575
var enrichedProperties = new TestLogEnrichmentTagCollector();
7676

7777
// Act

0 commit comments

Comments
 (0)