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)
- builder.Services.AddApplicationInsightsTelemetry(options => { options.ConnectionString = appSettings["AppInsightsConnectionString"]; });Answer
- builder.Services.AddSingleton<ITelemetryInitializer, RegionTelemetryInitializer>();Answer
- Cbuilder.Services.Configure<TelemetryConfiguration>(config => { config.InstrumentationKey = appSettings["AppInsightsConnectionString"]; });
- Dbuilder.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
Key Concept
Configuring Application Insights via ConnectionString and registering custom telemetry initializers using Dependency Injection in ASP.NET Core.
Estimated Time:2m 0s