Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing a C# ASP.NET Core Web API that will be hosted on Azure App Service. You want to programmatically configure the Application Insights SDK using connection strings read from your configuration provider. You add the Microsoft.ApplicationInsights.AspNetCore NuGet package to your project and write the following initialization code in Program.cs:

csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationInsightsTelemetry(options =>
{
// Configure telemetry connection string
});

var app = builder.Build();

Which of the following statements should you place inside the action delegate to correctly configure the telemetry connection string?

  1. options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];Answer
  2. B
    options.InstrumentationKey = builder.Configuration["ApplicationInsights:InstrumentationKey"];
  3. C
    options.TelemetryConfiguration.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
  4. D
    options.Context.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];

Answer

options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
The correct option sets the ConnectionString property directly on the ApplicationInsightsServiceOptions object. When configuring Application Insights in ASP.NET Core applications using the SDK, this property must be set to ensure telemetry is sent to the correct resource endpoint.

Step-by-Step Solution

1
Analyze the properties of the ApplicationInsightsServiceOptions class passed to AddApplicationInsightsTelemetry.
The options class provides a direct ConnectionString property to specify ingestion endpoints.
This allows developers to configure the SDK connection string programmatically during service registration.
2
Avoid deprecated properties like InstrumentationKey.
Ensure ConnectionString is used instead of InstrumentationKey.
Microsoft has deprecated instrumentation keys in favor of connection strings, which support endpoint routing and secure token authentication.
3
Assign the configuration value using builder.Configuration.
Set options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"].
This correctly pulls the setting from the configuration providers (such as appsettings.json or environment variables) and assigns it to the SDK options.

Key Concept

Programmatic configuration of Application Insights SDK using connection strings.
Estimated Time:1m 30s
Rate this question