From 987f999a26d1fde61e51f131d822b26e1eb0d0c6 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Mon, 2 Mar 2026 16:41:13 -0800 Subject: [PATCH 1/2] connection string --- .../Extensibility/TelemetryConfiguration.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs b/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs index 5fdb3a10a..895e3196b 100644 --- a/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs +++ b/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs @@ -357,6 +357,11 @@ internal void SetCloudRole(string serviceName, string serviceInstanceId = null, internal FeatureMetricEmissionHelper InitializeFeatureReporter() { + if (string.IsNullOrEmpty(this.connectionString)) + { + return this.FeatureReporter; + } + var connectionString = Microsoft.ApplicationInsights.Internal.ConnectionString.Parse(this.ConnectionString); var ciKey = connectionString.GetNonRequired("InstrumentationKey"); this.FeatureReporter = FeatureMetricEmissionHelper.GetOrCreate(ciKey, this.extensionVersion); @@ -381,6 +386,16 @@ internal OpenTelemetrySdk Build() return this.openTelemetrySdk; } + // If no connection string was explicitly set, fall back to the environment variable. + if (string.IsNullOrEmpty(this.connectionString)) + { + var envConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); + if (!string.IsNullOrEmpty(envConnectionString)) + { + this.connectionString = envConnectionString; + } + } + Environment.SetEnvironmentVariable("OTEL_SDK_DISABLED", this.disableTelemetry ? "true" : "false"); this.openTelemetrySdk = OpenTelemetrySdk.Create(builder => @@ -388,7 +403,10 @@ internal OpenTelemetrySdk Build() this.builderConfiguration(builder); builder.SetAzureMonitorExporter(options => { - options.ConnectionString = this.connectionString; + if (!string.IsNullOrEmpty(this.connectionString)) + { + options.ConnectionString = this.connectionString; + } if (this.tracesPerSecond.HasValue) { From d5c3c12271b93a03355daafd7a1b7329bda2ef44 Mon Sep 17 00:00:00 2001 From: Harsimar Kaur Date: Tue, 3 Mar 2026 10:19:21 -0800 Subject: [PATCH 2/2] nlog --- LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs index 47c108826..df61d9523 100644 --- a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs @@ -126,6 +126,11 @@ protected override void InitializeTarget() base.InitializeTarget(); string connectionString = this.connectionStringLayout.Render(LogEventInfo.CreateNullEvent()); + if (string.IsNullOrWhiteSpace(connectionString)) + { + connectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); + } + if (string.IsNullOrWhiteSpace(connectionString)) { throw new NLogConfigurationException(ConnectionStringRequiredMessage);