Skip to content

Commit a0a2e19

Browse files
fix: standardize AppInsights on APPLICATIONINSIGHTS_CONNECTION_STRING
Root cause: workflow was setting ApplicationInsights__ConnectionString (double-underscore hierarchical .NET config key) which does NOT match the flat env var that UseAzureMonitor() and Aspire's AzureApplicationInsightsResource both expect. - workflow: rename ApplicationInsights__ConnectionString -> APPLICATIONINSIGHTS_CONNECTION_STRING (both Staging and Production deploy steps; secret reference unchanged) - Program.cs: remove 3-key fallback chain; single builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] check; simplify UseAzureMonitor() to no-arg form (auto-reads the canonical env var) Official docs: 'The Azure Monitor distro expects the environment variable to be APPLICATIONINSIGHTS_CONNECTION_STRING' (learn.microsoft.com/dotnet/aspire)
1 parent c9acf63 commit a0a2e19

2 files changed

Lines changed: 4 additions & 11 deletions

File tree

.github/workflows/Build-Test-And-Deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ jobs:
166166
az containerapp update --name $CONTAINER_APP_NAME --resource-group $RESOURCEGROUP --replace-env-vars Authentication__github__clientId=secretref:github-clientid Authentication__github__clientSecret=secretref:github-clientsecret \
167167
Authentication__microsoft__clientId=secretref:msft-clientid Authentication__microsoft__clientSecret=secretref:msft-clientsecret AuthMessageSender__ApiKey=secretref:emailsender-apikey AuthMessageSender__SecretKey=secretref:emailsender-secret \
168168
AuthMessageSender__SendFromName=secretref:emailsender-name AuthMessageSender__SendFromEmail=secretref:emailsender-email ConnectionStrings__EssentialCSharpWebContextConnection=secretref:connectionstring ASPNETCORE_ENVIRONMENT=Staging \
169-
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring \
169+
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey APPLICATIONINSIGHTS_CONNECTION_STRING=secretref:appinsights-connectionstring \
170170
AIOptions__Endpoint=secretref:ai-endpoint AIOptions__ApiKey=secretref:ai-apikey AIOptions__VectorGenerationDeploymentName=secretref:ai-vectordeployment AIOptions__ChatDeploymentName=secretref:ai-chatdeployment \
171171
AIOptions__SystemPrompt=secretref:ai-systemprompt ConnectionStrings__PostgresVectorStore=secretref:postgres-vectorstore-connectionstring \
172172
TryDotNet__Origin=$TRYDOTNET_ORIGIN
@@ -263,7 +263,7 @@ jobs:
263263
az containerapp update --name $CONTAINER_APP_NAME --resource-group $RESOURCEGROUP --replace-env-vars Authentication__github__clientId=secretref:github-clientid Authentication__github__clientSecret=secretref:github-clientsecret \
264264
Authentication__microsoft__clientId=secretref:msft-clientid Authentication__microsoft__clientSecret=secretref:msft-clientsecret AuthMessageSender__ApiKey=secretref:emailsender-apikey AuthMessageSender__SecretKey=secretref:emailsender-secret \
265265
AuthMessageSender__SendFromName=secretref:emailsender-name AuthMessageSender__SendFromEmail=secretref:emailsender-email ConnectionStrings__EssentialCSharpWebContextConnection=secretref:connectionstring ASPNETCORE_ENVIRONMENT=Production \
266-
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring \
266+
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey APPLICATIONINSIGHTS_CONNECTION_STRING=secretref:appinsights-connectionstring \
267267
AIOptions__Endpoint=secretref:ai-endpoint AIOptions__ApiKey=secretref:ai-apikey AIOptions__VectorGenerationDeploymentName=secretref:ai-vectordeployment AIOptions__ChatDeploymentName=secretref:ai-chatdeployment \
268268
AIOptions__SystemPrompt=secretref:ai-systemprompt ConnectionStrings__PostgresVectorStore=secretref:postgres-vectorstore-connectionstring \
269269
TryDotNet__Origin=$TRYDOTNET_ORIGIN

EssentialCSharp.Web/Program.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,7 @@ private static void Main(string[] args)
3838
// Production: Azure Monitor (Application Insights) via APPLICATIONINSIGHTS_CONNECTION_STRING
3939
// Local/Aspire: OTLP to Aspire Dashboard via OTEL_EXPORTER_OTLP_ENDPOINT
4040
// Never both simultaneously — that would cause duplicate telemetry in App Insights.
41-
// Resolve from all config locations:
42-
// 1. Standard Azure Monitor flat env var (SDK's own default key)
43-
// 2. Double-underscore (hierarchical) format set by the deployment workflow
44-
// 3. GetConnectionString fallback
45-
string? appInsightsConnectionString =
46-
builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]
47-
?? builder.Configuration["ApplicationInsights:ConnectionString"]
48-
?? builder.Configuration.GetConnectionString("ApplicationInsights");
41+
string? appInsightsConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
4942
bool useAzureMonitor = !string.IsNullOrWhiteSpace(appInsightsConnectionString);
5043
bool useOtlp = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
5144

@@ -88,7 +81,7 @@ private static void Main(string[] args)
8881
});
8982

9083
if (useAzureMonitor)
91-
otel.UseAzureMonitor(options => options.ConnectionString = appInsightsConnectionString);
84+
otel.UseAzureMonitor();
9285
else if (useOtlp)
9386
otel.UseOtlpExporter();
9487

0 commit comments

Comments
 (0)