Question

Difficulty: HardConfigure Application Insights Instrumentation and Telemetry

You are developing a C# .NET 8 isolated worker Azure Function App. To meet your company's security compliance, the Application Insights connection string must be retrieved at startup from an Azure Key Vault using the Azure Identity library, rather than being stored in plain text in application settings.

In `Program.cs`, you retrieve the connection string value from Key Vault and store it in a variable named `vaultConnectionString`.

Which code segment should you use to register Application Insights telemetry with this connection string in the dependency injection container?

  1. A
    csharp
    TelemetryConfiguration.Active.ConnectionString = vaultConnectionString;
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
  2. csharp
    services.AddApplicationInsightsTelemetryWorkerService(options =>
    {
    options.ConnectionString = vaultConnectionString;
    });
    services.ConfigureFunctionsApplicationInsights();
    Answer
  3. C
    csharp
    services.ConfigureFunctionsApplicationInsights(options =>
    {
    options.ConnectionString = vaultConnectionString;
    });
  4. D
    csharp
    services.AddApplicationInsightsTelemetryWorkerService(options =>
    {
    options.InstrumentationKey = vaultConnectionString;
    });
    services.ConfigureFunctionsApplicationInsights();

Answer

The correct option is the one that sets the `ConnectionString` property of `ApplicationInsightsServiceOptions` inside `AddApplicationInsightsTelemetryWorkerService` and then calls `ConfigureFunctionsApplicationInsights`.
The correct option programmatically sets the connection string via the configuration lambda in `AddApplicationInsightsTelemetryWorkerService`. In .NET isolated worker Azure Functions, the `AddApplicationInsightsTelemetryWorkerService` extension method is used to register the telemetry services, and passing an action configuring `ApplicationInsightsServiceOptions.ConnectionString` is the supported way to provide the connection string programmatically. Calling `ConfigureFunctionsApplicationInsights` afterwards integrates the worker telemetry with the Functions host.

Step-by-Step Solution

1
Configure the options for `AddApplicationInsightsTelemetryWorkerService`.
Specifies the target connection string using the configuration options.
Allows the underlying telemetry service to route telemetry data to the correct Application Insights resource.
2
Call `ConfigureFunctionsApplicationInsights()`.
Integrates the Application Insights worker service with the Azure Functions host telemetry.
Ensures functions-specific tracking (like invocation success/failure and execution logs) is correctly correlated.

Key Concept

Programmatic configuration of Application Insights Connection String in Azure Functions Isolated Worker
Rate this question