diff --git a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt index 0632fc5c43..04d1e99149 100644 --- a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt @@ -9,8 +9,6 @@ Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOp Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.Credential.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DependencyCollectionOptions.get -> Microsoft.ApplicationInsights.AspNetCore.Extensions.DependencyCollectionOptions -Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DeveloperMode.get -> bool? -Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.DeveloperMode.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool diff --git a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt index 01876d9d72..a72838a8a7 100644 --- a/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt @@ -9,8 +9,6 @@ Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Co Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.get -> Azure.Core.TokenCredential Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Credential.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DependencyCollectionOptions.get -> Microsoft.ApplicationInsights.WorkerService.DependencyCollectionOptions -Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DeveloperMode.get -> bool? -Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.DeveloperMode.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.get -> bool Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableAdaptiveSampling.set -> void Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDebugLogger.get -> bool diff --git a/CHANGELOG.md b/CHANGELOG.md index 257403c7bd..b1328baf90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ### Added +- [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) - [Added support for Entra ID (Azure Active Directory) authentication using Azure.Core.TokenCredential](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3054) ## Version 3.0.0-beta1 diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 158824164b..3bd29b38f8 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -82,6 +82,13 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo { if (!IsApplicationInsightsAdded(services)) { + // Register the default configuration options to automatically read from appsettings.json + services.AddOptions() + .Configure((options, config) => + { + AddTelemetryConfiguration(config, options); + }); + services.AddOpenTelemetry() .WithApplicationInsights() .UseApplicationInsightsTelemetry(); diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index 719dcec947..36a3c134c4 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -81,6 +81,13 @@ public static IServiceCollection AddApplicationInsightsTelemetryWorkerService(th { if (!IsApplicationInsightsAdded(services)) { + // Register the default configuration options to automatically read from appsettings.json + services.AddOptions() + .Configure((options, config) => + { + AddTelemetryConfiguration(config, options); + }); + services.AddOpenTelemetry() .WithApplicationInsights() .UseApplicationInsightsTelemetry(); @@ -187,10 +194,7 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen exporterOptions.SamplingRatio = 1.0F; } - if (serviceOptions.EnableQuickPulseMetricStream) - { - exporterOptions.EnableLiveMetrics = true; - } + exporterOptions.EnableLiveMetrics = serviceOptions.EnableQuickPulseMetricStream; }); builder.UseAzureMonitorExporter(); diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs index 608d7d2286..a3831e6694 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs @@ -29,15 +29,10 @@ public static partial class ApplicationInsightsExtensions { private const string VersionKeyFromConfig = "version"; private const string ConnectionStringFromConfig = "ApplicationInsights:ConnectionString"; - private const string DeveloperModeFromConfig = "ApplicationInsights:TelemetryChannel:DeveloperMode"; - private const string EndpointAddressFromConfig = "ApplicationInsights:TelemetryChannel:EndpointAddress"; private const string ConnectionStringEnvironmentVariable = "APPLICATIONINSIGHTS_CONNECTION_STRING"; - private const string DeveloperModeForWebSites = "APPINSIGHTS_DEVELOPER_MODE"; - private const string EndpointAddressForWebSites = "APPINSIGHTS_ENDPOINTADDRESS"; private const string ApplicationInsightsSectionFromConfig = "ApplicationInsights"; - private const string TelemetryChannelSectionFromConfig = "ApplicationInsights:TelemetryChannel"; /// /// Read configuration from appSettings.json, appsettings.{env.EnvironmentName}.json, @@ -54,26 +49,12 @@ internal static void AddTelemetryConfiguration( try { config.GetSection(ApplicationInsightsSectionFromConfig).Bind(serviceOptions); - config.GetSection(TelemetryChannelSectionFromConfig).Bind(serviceOptions); if (config.TryGetValue(primaryKey: ConnectionStringEnvironmentVariable, backupKey: ConnectionStringFromConfig, value: out string connectionStringValue)) { serviceOptions.ConnectionString = connectionStringValue; } - if (config.TryGetValue(primaryKey: DeveloperModeForWebSites, backupKey: DeveloperModeFromConfig, value: out string developerModeValue)) - { - if (bool.TryParse(developerModeValue, out bool developerMode)) - { - serviceOptions.DeveloperMode = developerMode; - } - } - - if (config.TryGetValue(primaryKey: EndpointAddressForWebSites, backupKey: EndpointAddressFromConfig, value: out string endpointAddress)) - { - serviceOptions.EndpointAddress = endpointAddress; - } - if (config.TryGetValue(primaryKey: VersionKeyFromConfig, value: out string version)) { serviceOptions.ApplicationVersion = version; diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs index 810b958009..c5809c52a9 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs @@ -55,11 +55,6 @@ public class ApplicationInsightsServiceOptions /// public string ApplicationVersion { get; set; } = Assembly.GetEntryAssembly()?.GetName().Version.ToString(); - /// - /// Gets or sets a value indicating whether telemetry channel should be set to developer mode. - /// - public bool? DeveloperMode { get; set; } - /// /// Gets or sets the endpoint address of the channel. /// @@ -117,11 +112,6 @@ public class ApplicationInsightsServiceOptions /// Target instance to copy properties to. internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target) { - if (this.DeveloperMode != null) - { - target.DeveloperMode = this.DeveloperMode; - } - if (!string.IsNullOrEmpty(this.EndpointAddress)) { target.EndpointAddress = this.EndpointAddress; diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs b/NETCORE/src/Shared/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs similarity index 70% rename from NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs rename to NETCORE/src/Shared/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs index 7d16ca2b49..0777bc7917 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs +++ b/NETCORE/src/Shared/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs @@ -1,15 +1,20 @@ -namespace Microsoft.ApplicationInsights.WorkerService +#if AI_ASPNETCORE_WEB +namespace Microsoft.AspNetCore.Hosting +#else +namespace Microsoft.ApplicationInsights.WorkerService +#endif { - using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +#if AI_ASPNETCORE_WEB + using Microsoft.ApplicationInsights.AspNetCore.Extensions; +#endif using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; /// - /// implementation that reads options from provided IConfiguration. + /// implementation that reads options from provided IConfiguration. /// - [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "This class is instantiated by Dependency Injection.")] internal class DefaultApplicationInsightsServiceConfigureOptions : IConfigureOptions { private readonly IConfiguration configuration; @@ -30,11 +35,6 @@ public void Configure(ApplicationInsightsServiceOptions options) { ApplicationInsightsExtensions.AddTelemetryConfiguration(this.configuration, options); } - - if (Debugger.IsAttached) - { - options.DeveloperMode = true; - } } } } diff --git a/NETCORE/test/IntegrationTests.Tests/IntegrationTests.Tests.csproj b/NETCORE/test/IntegrationTests.Tests/IntegrationTests.Tests.csproj index 3c64d4fe09..d0824e2426 100644 --- a/NETCORE/test/IntegrationTests.Tests/IntegrationTests.Tests.csproj +++ b/NETCORE/test/IntegrationTests.Tests/IntegrationTests.Tests.csproj @@ -12,6 +12,7 @@ net10.0;net9.0;net8.0; + $(DefineConstants);AI_ASPNETCORE_WEB @@ -28,10 +29,17 @@ + + + + PreserveNewest + + Always + diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json new file mode 100644 index 0000000000..45340f7881 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-default.json @@ -0,0 +1,15 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint", + "EnableAdaptiveSampling": false, + "EnableQuickPulseMetricStream": true, + "ApplicationVersion": "Version", + "RequestCollectionOptions": { + "InjectResponseHeaders": true, + "TrackExceptions": false + }, + "DependencyCollectionOptions": { + "EnableLegacyCorrelationHeadersInjection": false + } + } +} \ No newline at end of file diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json new file mode 100644 index 0000000000..4346f32091 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-false.json @@ -0,0 +1,14 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555", + "EnableAdaptiveSampling": false, + "EnableQuickPulseMetricStream": false, + "RequestCollectionOptions": { + "InjectResponseHeaders": false, + "TrackExceptions": false + }, + "DependencyCollectionOptions": { + "EnableLegacyCorrelationHeadersInjection": false + } + } +} diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json new file mode 100644 index 0000000000..e1c54cc937 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-all-settings-true.json @@ -0,0 +1,15 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=22222222-2222-3333-4444-555555555555", + "EnableAdaptiveSampling": true, + "EnableQuickPulseMetricStream": true, + "ApplicationVersion": "1.0.0", + "RequestCollectionOptions": { + "InjectResponseHeaders": true, + "TrackExceptions": true + }, + "DependencyCollectionOptions": { + "EnableLegacyCorrelationHeadersInjection": true + } + } +} diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-connection-string.json b/NETCORE/test/IntegrationTests.Tests/content/config-connection-string.json new file mode 100644 index 0000000000..fc7b76c160 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-connection-string.json @@ -0,0 +1,5 @@ +{ + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://127.0.0.1" + } +} diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-false.json b/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-false.json new file mode 100644 index 0000000000..cb69a0c0c4 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-false.json @@ -0,0 +1,12 @@ +{ + "ApplicationInsights": { + "RequestCollectionOptions": { + "InjectResponseHeaders": false, + "TrackExceptions": false, + "EnableW3CDistributedTracing": false + }, + "DependencyCollectionOptions": { + "EnableLegacyCorrelationHeadersInjection": false + } + } +} diff --git a/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-true.json b/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-true.json new file mode 100644 index 0000000000..8efa9b6f47 --- /dev/null +++ b/NETCORE/test/IntegrationTests.Tests/content/config-req-dep-settings-true.json @@ -0,0 +1,12 @@ +{ + "ApplicationInsights": { + "RequestCollectionOptions": { + "InjectResponseHeaders": true, + "TrackExceptions": true, + "EnableW3CDistributedTracing": true + }, + "DependencyCollectionOptions": { + "EnableLegacyCorrelationHeadersInjection": true + } + } +} diff --git a/NETCORE/test/Shared/ConfigurationTests.cs b/NETCORE/test/Shared/ConfigurationTests.cs new file mode 100644 index 0000000000..a123902c8f --- /dev/null +++ b/NETCORE/test/Shared/ConfigurationTests.cs @@ -0,0 +1,245 @@ +using System; +using System.IO; +using Azure.Monitor.OpenTelemetry.Exporter; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Xunit; +using Xunit.Abstractions; + +#if AI_ASPNETCORE_WEB +namespace Microsoft.ApplicationInsights.AspNetCore.Tests +{ + using Microsoft.ApplicationInsights.AspNetCore.Extensions; +#else +namespace Microsoft.ApplicationInsights.WorkerService.Tests +{ + using Microsoft.ApplicationInsights.WorkerService; +#endif + + public class ConfigurationTests + { + private readonly ITestOutputHelper output; + private const string TestConnectionString = "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://127.0.0.1"; + + public ConfigurationTests(ITestOutputHelper output) + { + this.output = output; + } + + [Fact] + public void ReadsConnectionStringFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-connection-string.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal(TestConnectionString, options.ConnectionString); + } + + [Fact] + public void ReadsEnableAdaptiveSamplingFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-false.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.False(options.EnableAdaptiveSampling); + } + + [Fact] + public void ReadsEnableQuickPulseMetricStreamFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-false.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.False(options.EnableQuickPulseMetricStream); + } + + [Fact] + public void ReadsApplicationVersionFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-true.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal("1.0.0", options.ApplicationVersion); + } + + [Fact] + public void ConfigurationFlowsFromApplicationInsightsSectionToAzureMonitorExporter() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-false.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + + // Verify ApplicationInsightsServiceOptions + var aiOptions = serviceProvider.GetRequiredService>().Value; + Assert.Equal("InstrumentationKey=22222222-2222-3333-4444-555555555555", aiOptions.ConnectionString); + Assert.False(aiOptions.EnableAdaptiveSampling); + Assert.False(aiOptions.EnableQuickPulseMetricStream); + + // Verify AzureMonitorExporterOptions gets the values + var exporterOptions = serviceProvider.GetRequiredService>().Value; + Assert.Equal("InstrumentationKey=22222222-2222-3333-4444-555555555555", exporterOptions.ConnectionString); + Assert.Equal(1.0F, exporterOptions.SamplingRatio); // No sampling when EnableAdaptiveSampling is false + Assert.False(exporterOptions.EnableLiveMetrics); + } + + [Fact] + public void EnvironmentVariablesTakePrecedenceOverAppSettings() + { + // ARRANGE + const string envConnectionString = "InstrumentationKey=AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE;IngestionEndpoint=http://env-endpoint"; + Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING", envConnectionString); + + try + { + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-connection-string.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).AddEnvironmentVariables().Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(); +#else + services.AddApplicationInsightsTelemetryWorkerService(); +#endif + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal(envConnectionString, options.ConnectionString); + } + finally + { + Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING", null); + } + } + + [Fact] + public void ExplicitConfigurationTakesPrecedenceOverDefaultConfiguration() + { + // ARRANGE + const string explicitConnectionString = "InstrumentationKey=CCCCCCCC-DDDD-EEEE-FFFF-111111111111;IngestionEndpoint=http://explicit-endpoint"; + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-connection-string.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT - Pass explicit options +#if AI_ASPNETCORE_WEB + services.AddApplicationInsightsTelemetry(options => +#else + services.AddApplicationInsightsTelemetryWorkerService(options => +#endif + { + options.ConnectionString = explicitConnectionString; + }); + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.Equal(explicitConnectionString, options.ConnectionString); + } + +#if AI_ASPNETCORE_WEB + [Fact] + public void ReadsRequestCollectionOptionsFromApplicationInsightsSectionInConfig() + { + // ARRANGE + var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), "content", "config-all-settings-false.json"); + this.output.WriteLine("json:" + jsonFullPath); + var config = new ConfigurationBuilder().AddJsonFile(jsonFullPath).Build(); + + var services = new ServiceCollection(); + services.AddSingleton(config); + + // ACT + services.AddApplicationInsightsTelemetry(); + + // VALIDATE + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>().Value; + Assert.False(options.RequestCollectionOptions.InjectResponseHeaders); + Assert.False(options.RequestCollectionOptions.TrackExceptions); + } +#endif + } +} diff --git a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerIntegrationTests.Tests.csproj b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerIntegrationTests.Tests.csproj index 36fda64619..86cd425fdf 100644 --- a/NETCORE/test/WorkerIntegrationTests.Tests/WorkerIntegrationTests.Tests.csproj +++ b/NETCORE/test/WorkerIntegrationTests.Tests/WorkerIntegrationTests.Tests.csproj @@ -19,10 +19,17 @@ + + + + PreserveNewest + + Always + diff --git a/examples/AspNetCoreWebApp/appsettings.json b/examples/AspNetCoreWebApp/appsettings.json index d9d9a9bff6..90798873f3 100644 --- a/examples/AspNetCoreWebApp/appsettings.json +++ b/examples/AspNetCoreWebApp/appsettings.json @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://testendpoint" + } }