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?
- options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];Answer
- Boptions.InstrumentationKey = builder.Configuration["ApplicationInsights:InstrumentationKey"];
- Coptions.TelemetryConfiguration.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
- Doptions.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
Key Concept
Programmatic configuration of Application Insights SDK using connection strings.
Estimated Time:1m 30s