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) { 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);