Skip to content

Commit ed2d9bc

Browse files
evgenyfedorov2evgenyfedorov2
authored andcommitted
Initial commit
1 parent d323577 commit ed2d9bc

File tree

11 files changed

+187
-36
lines changed

11 files changed

+187
-36
lines changed

docs/list-of-diagnostics.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ At some point in its lifecycle APIs become obsolete. If you use obsolete APIs, y
4848

4949
You may continue using obsolete APIs in your application, but we advise exploring proposed alternatives which you will find in the obsoletion message.
5050

51-
| Diagnostic ID | Description |
52-
| :---------------- | :---------- |
51+
| Diagnostic ID | Description |
52+
| :---------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
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: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.Diagnostics.Enrichment;
8+
using Microsoft.Shared.DiagnosticIds;
79
using Microsoft.Shared.Diagnostics;
810

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

2670
return services
27-
.AddServiceLogEnricher(_ => { });
71+
.AddApplicationLogEnricher(_ => { });
2872
}
2973

3074
/// <summary>
31-
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
75+
/// Adds an instance of the application enricher to the <see cref="IServiceCollection"/>.
3276
/// </summary>
33-
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
77+
/// <param name="services">The <see cref="IServiceCollection"/> to add the application enricher to.</param>
3478
/// <param name="configure">The <see cref="ApplicationLogEnricherOptions"/> configuration delegate.</param>
3579
/// <returns>The value of <paramref name="services"/>.</returns>
3680
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
37-
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure)
81+
[Experimental(diagnosticId: DiagnosticIds.Experiments.Telemetry, UrlFormat = DiagnosticIds.UrlFormat)]
82+
public static IServiceCollection AddApplicationLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure)
3883
{
3984
_ = Throw.IfNull(services);
4085
_ = Throw.IfNull(configure);
@@ -45,13 +90,14 @@ public static IServiceCollection AddServiceLogEnricher(this IServiceCollection s
4590
}
4691

4792
/// <summary>
48-
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
93+
/// Adds an instance of the application enricher to the <see cref="IServiceCollection"/>.
4994
/// </summary>
50-
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
95+
/// <param name="services">The <see cref="IServiceCollection"/> to add the application enricher to.</param>
5196
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ApplicationLogEnricherOptions"/> in the service enricher.</param>
5297
/// <returns>The value of <paramref name="services"/>.</returns>
5398
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
54-
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, IConfigurationSection section)
99+
[Experimental(diagnosticId: DiagnosticIds.Experiments.Telemetry, UrlFormat = DiagnosticIds.UrlFormat)]
100+
public static IServiceCollection AddApplicationLogEnricher(this IServiceCollection services, IConfigurationSection section)
55101
{
56102
_ = Throw.IfNull(services);
57103
_ = 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<RootNamespace>Microsoft.Extensions.Diagnostics</RootNamespace>
44
<Description>Provides canonical implementations of telemetry abstractions.</Description>
55
<Workstream>Telemetry</Workstream>
6+
<NoWarn Condition="'$(TargetFramework)' == 'net462'">$(NoWarn);CS0436</NoWarn>
67
</PropertyGroup>
78

89
<PropertyGroup>
@@ -17,6 +18,7 @@
1718
<InjectSharedNumericExtensions>true</InjectSharedNumericExtensions>
1819
<InjectSharedPools>true</InjectSharedPools>
1920
<InjectSharedRentedSpan>true</InjectSharedRentedSpan>
21+
<InjectObsoleteAttributeOnLegacy>true</InjectObsoleteAttributeOnLegacy>
2022
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
2123
<UseLoggingGenerator>true</UseLoggingGenerator>
2224
</PropertyGroup>

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)