Skip to content

Commit

Permalink
Support using connectionString for ApplicationInsights (#379)
Browse files Browse the repository at this point in the history
This wil become a requirement from may 2025, so adding this early ensures that does not become an requirement for updating apps.

microsoft/ApplicationInsights-dotnet#2560
  • Loading branch information
ivarne authored Dec 29, 2023
1 parent c018446 commit 97da907
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,29 @@ public static void AddAltinnAppServices(this IServiceCollection services, IConfi

private static void AddApplicationInsights(IServiceCollection services, IConfiguration config, IWebHostEnvironment env)
{
string applicationInsightsKey = env.IsDevelopment() ?
config["ApplicationInsights:InstrumentationKey"]
: Environment.GetEnvironmentVariable("ApplicationInsights__InstrumentationKey");

if (!string.IsNullOrEmpty(applicationInsightsKey))
string? applicationInsightsKey = env.IsDevelopment()
? config["ApplicationInsights:InstrumentationKey"]
: Environment.GetEnvironmentVariable("ApplicationInsights__InstrumentationKey");
string? applicationInsightsConnectionString = env.IsDevelopment() ?
config["ApplicationInsights:ConnectionString"]
: Environment.GetEnvironmentVariable("ApplicationInsights__ConnectionString");

if (!string.IsNullOrEmpty(applicationInsightsKey) || !string.IsNullOrEmpty(applicationInsightsConnectionString))
{
services.AddApplicationInsightsTelemetry(applicationInsightsKey);
services.AddApplicationInsightsTelemetry((options) =>
{
if (string.IsNullOrEmpty(applicationInsightsConnectionString))
{
#pragma warning disable CS0618 // Type or member is obsolete
// Set instrumentationKey for compatibility if connectionString does not exist.
options.InstrumentationKey = applicationInsightsKey;
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
options.ConnectionString = applicationInsightsConnectionString;
}
});
services.AddApplicationInsightsTelemetryProcessor<IdentityTelemetryFilter>();
services.AddApplicationInsightsTelemetryProcessor<HealthTelemetryFilter>();
services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
Expand Down

0 comments on commit 97da907

Please sign in to comment.