Skip to content

Commit 1648003

Browse files
Merge pull request #3064 from microsoft/rajrang/supportConfig
Add automatic configuration reading from ApplicationInsights section in appsettings.json
2 parents 16c877a + 4a95f32 commit 1648003

File tree

18 files changed

+362
-47
lines changed

18 files changed

+362
-47
lines changed

.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOp
99
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential
1010
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.set -> void
1111
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DependencyCollectionOptions.get -> Microsoft.ApplicationInsights.AspNetCore.Extensions.DependencyCollectionOptions
12-
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DeveloperMode.get -> bool?
13-
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DeveloperMode.set -> void
1412
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool
1513
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void
1614
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool

.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Co
99
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential
1010
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.set -> void
1111
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DependencyCollectionOptions.get -> Microsoft.ApplicationInsights.WorkerService.DependencyCollectionOptions
12-
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DeveloperMode.get -> bool?
13-
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DeveloperMode.set -> void
1412
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool
1513
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.set -> void
1614
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDebugLogger.get -> bool

CHANGELOG.md

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

33
## Unreleased
44
### Added
5+
- [Automatic configuration binding from "ApplicationInsights" section in appsettings.json for both AspNetCore and WorkerService packages with configuration precedence: environment variables > explicit configuration > appsettings.json](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3064)
56
- [Added support for Entra ID (Azure Active Directory) authentication using Azure.Core.TokenCredential](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3054)
67

78
## Version 3.0.0-beta1

NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo
8282
{
8383
if (!IsApplicationInsightsAdded(services))
8484
{
85+
// Register the default configuration options to automatically read from appsettings.json
86+
services.AddOptions<ApplicationInsightsServiceOptions>()
87+
.Configure<IConfiguration>((options, config) =>
88+
{
89+
AddTelemetryConfiguration(config, options);
90+
});
91+
8592
services.AddOpenTelemetry()
8693
.WithApplicationInsights()
8794
.UseApplicationInsightsTelemetry();

NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public static IServiceCollection AddApplicationInsightsTelemetryWorkerService(th
8181
{
8282
if (!IsApplicationInsightsAdded(services))
8383
{
84+
// Register the default configuration options to automatically read from appsettings.json
85+
services.AddOptions<ApplicationInsightsServiceOptions>()
86+
.Configure<IConfiguration>((options, config) =>
87+
{
88+
AddTelemetryConfiguration(config, options);
89+
});
90+
8491
services.AddOpenTelemetry()
8592
.WithApplicationInsights()
8693
.UseApplicationInsightsTelemetry();
@@ -187,10 +194,7 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen
187194
exporterOptions.SamplingRatio = 1.0F;
188195
}
189196

190-
if (serviceOptions.EnableQuickPulseMetricStream)
191-
{
192-
exporterOptions.EnableLiveMetrics = true;
193-
}
197+
exporterOptions.EnableLiveMetrics = serviceOptions.EnableQuickPulseMetricStream;
194198
});
195199

196200
builder.UseAzureMonitorExporter();

NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ public static partial class ApplicationInsightsExtensions
2929
{
3030
private const string VersionKeyFromConfig = "version";
3131
private const string ConnectionStringFromConfig = "ApplicationInsights:ConnectionString";
32-
private const string DeveloperModeFromConfig = "ApplicationInsights:TelemetryChannel:DeveloperMode";
33-
private const string EndpointAddressFromConfig = "ApplicationInsights:TelemetryChannel:EndpointAddress";
3432

3533
private const string ConnectionStringEnvironmentVariable = "APPLICATIONINSIGHTS_CONNECTION_STRING";
36-
private const string DeveloperModeForWebSites = "APPINSIGHTS_DEVELOPER_MODE";
37-
private const string EndpointAddressForWebSites = "APPINSIGHTS_ENDPOINTADDRESS";
3834

3935
private const string ApplicationInsightsSectionFromConfig = "ApplicationInsights";
40-
private const string TelemetryChannelSectionFromConfig = "ApplicationInsights:TelemetryChannel";
4136

4237
/// <summary>
4338
/// Read configuration from appSettings.json, appsettings.{env.EnvironmentName}.json,
@@ -54,26 +49,12 @@ internal static void AddTelemetryConfiguration(
5449
try
5550
{
5651
config.GetSection(ApplicationInsightsSectionFromConfig).Bind(serviceOptions);
57-
config.GetSection(TelemetryChannelSectionFromConfig).Bind(serviceOptions);
5852

5953
if (config.TryGetValue(primaryKey: ConnectionStringEnvironmentVariable, backupKey: ConnectionStringFromConfig, value: out string connectionStringValue))
6054
{
6155
serviceOptions.ConnectionString = connectionStringValue;
6256
}
6357

64-
if (config.TryGetValue(primaryKey: DeveloperModeForWebSites, backupKey: DeveloperModeFromConfig, value: out string developerModeValue))
65-
{
66-
if (bool.TryParse(developerModeValue, out bool developerMode))
67-
{
68-
serviceOptions.DeveloperMode = developerMode;
69-
}
70-
}
71-
72-
if (config.TryGetValue(primaryKey: EndpointAddressForWebSites, backupKey: EndpointAddressFromConfig, value: out string endpointAddress))
73-
{
74-
serviceOptions.EndpointAddress = endpointAddress;
75-
}
76-
7758
if (config.TryGetValue(primaryKey: VersionKeyFromConfig, value: out string version))
7859
{
7960
serviceOptions.ApplicationVersion = version;

NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public class ApplicationInsightsServiceOptions
5555
/// </summary>
5656
public string ApplicationVersion { get; set; } = Assembly.GetEntryAssembly()?.GetName().Version.ToString();
5757

58-
/// <summary>
59-
/// Gets or sets a value indicating whether telemetry channel should be set to developer mode.
60-
/// </summary>
61-
public bool? DeveloperMode { get; set; }
62-
6358
/// <summary>
6459
/// Gets or sets the endpoint address of the channel.
6560
/// </summary>
@@ -117,11 +112,6 @@ public class ApplicationInsightsServiceOptions
117112
/// <param name="target">Target instance to copy properties to.</param>
118113
internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target)
119114
{
120-
if (this.DeveloperMode != null)
121-
{
122-
target.DeveloperMode = this.DeveloperMode;
123-
}
124-
125115
if (!string.IsNullOrEmpty(this.EndpointAddress))
126116
{
127117
target.EndpointAddress = this.EndpointAddress;

NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs renamed to NETCORE/src/Shared/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
namespace Microsoft.ApplicationInsights.WorkerService
1+
#if AI_ASPNETCORE_WEB
2+
namespace Microsoft.AspNetCore.Hosting
3+
#else
4+
namespace Microsoft.ApplicationInsights.WorkerService
5+
#endif
26
{
3-
using System.Diagnostics;
47
using System.Diagnostics.CodeAnalysis;
8+
#if AI_ASPNETCORE_WEB
9+
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
10+
#endif
511
using Microsoft.Extensions.Configuration;
612
using Microsoft.Extensions.DependencyInjection;
713
using Microsoft.Extensions.Options;
814

915
/// <summary>
10-
/// <see cref="IConfigureOptions&lt;ApplicationInsightsServiceOptions&gt;"/> implementation that reads options from provided IConfiguration.
16+
/// <see cref="IConfigureOptions{ApplicationInsightsServiceOptions}"/> implementation that reads options from provided IConfiguration.
1117
/// </summary>
12-
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "This class is instantiated by Dependency Injection.")]
1318
internal class DefaultApplicationInsightsServiceConfigureOptions : IConfigureOptions<ApplicationInsightsServiceOptions>
1419
{
1520
private readonly IConfiguration configuration;
@@ -30,11 +35,6 @@ public void Configure(ApplicationInsightsServiceOptions options)
3035
{
3136
ApplicationInsightsExtensions.AddTelemetryConfiguration(this.configuration, options);
3237
}
33-
34-
if (Debugger.IsAttached)
35-
{
36-
options.DeveloperMode = true;
37-
}
3838
}
3939
}
4040
}

NETCORE/test/IntegrationTests.Tests/IntegrationTests.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PropertyGroup>
1313
<!-- TargetFrameworks are defined in Test.props, but can be overridden here if needed. -->
1414
<TargetFrameworks>net10.0;net9.0;net8.0;</TargetFrameworks>
15+
<DefineConstants>$(DefineConstants);AI_ASPNETCORE_WEB</DefineConstants>
1516
</PropertyGroup>
1617

1718
<ItemGroup>
@@ -28,10 +29,17 @@
2829
<ProjectReference Include="..\IntegrationTests.WebApp\IntegrationTests.WebApp.csproj" />
2930
</ItemGroup>
3031

32+
<ItemGroup>
33+
<Compile Include="..\Shared\ConfigurationTests.cs" Link="ConfigurationTests.cs" />
34+
</ItemGroup>
35+
3136
<ItemGroup>
3237
<None Update="xunit.runner.json">
3338
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3439
</None>
40+
<None Update="content\**">
41+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
42+
</None>
3543
</ItemGroup>
3644

3745
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"ApplicationInsights": {
3+
"ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint",
4+
"EnableAdaptiveSampling": false,
5+
"EnableQuickPulseMetricStream": true,
6+
"ApplicationVersion": "Version",
7+
"RequestCollectionOptions": {
8+
"InjectResponseHeaders": true,
9+
"TrackExceptions": false
10+
},
11+
"DependencyCollectionOptions": {
12+
"EnableLegacyCorrelationHeadersInjection": false
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)