Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing an ASP.NET Core web API hosted on Azure App Service (Linux). You need to configure the Application Insights SDK programmatically to send custom telemetry. You retrieve the connection string from Azure App Configuration. Additionally, you have implemented a custom telemetry initializer named `RegionTelemetryInitializer` to enrich all telemetry items with a `DeploymentRegion` property.

Which of the following configurations are required in the `Program.cs` file to ensure that custom telemetry is collected and properly enriched? (Select TWO)

  1. builder.Services.AddApplicationInsightsTelemetry(options => { options.ConnectionString = appSettings["AppInsightsConnectionString"]; });Answer
  2. builder.Services.AddSingleton<ITelemetryInitializer, RegionTelemetryInitializer>();Answer
  3. C
    builder.Services.Configure<TelemetryConfiguration>(config => { config.InstrumentationKey = appSettings["AppInsightsConnectionString"]; });
  4. D
    builder.Services.AddSingleton<TelemetryClient>(new TelemetryClient(new TelemetryConfiguration { InstrumentationKey = appSettings["AppInsightsConnectionString"] }));

Answer

To configure Application Insights with a custom initializer, you must register Application Insights using AddApplicationInsightsTelemetry while specifying the ConnectionString property, and register your custom RegionTelemetryInitializer as a singleton of type ITelemetryInitializer.
Calling AddApplicationInsightsTelemetry and configuring the ConnectionString ensures the SDK successfully initializes and targets the correct Azure resource. Registering the custom initializer as a singleton of ITelemetryInitializer allows the SDK to automatically intercept and enrich all collected telemetry with the custom property.

Step-by-Step Solution

1
Call AddApplicationInsightsTelemetry on builder.Services and supply the ConnectionString in the options delegate.
The Application Insights SDK is initialized with the correct connection string.
Connection strings are required to authenticate and route telemetry to the correct Log Analytics workspace.
2
Register the RegionTelemetryInitializer class as a singleton service for ITelemetryInitializer.
The SDK automatically resolves the initializer from the DI container.
Registered telemetry initializers are automatically executed for every telemetry item created by the TelemetryClient.

Key Concept

Configuring Application Insights via ConnectionString and registering custom telemetry initializers using Dependency Injection in ASP.NET Core.
Estimated Time:2m 0s
Rate this question