Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing a C# .NET 8 worker service hosted on Azure Container Apps. The service processes queue messages and is configured to send telemetry to Application Insights.

In Program.cs, you register the telemetry services:
csharp
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddApplicationInsightsTelemetryWorkerService(options =>
{
options.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
});

Within a separate processor class, you instantiate the telemetry client manually to log custom events:
csharp
public class QueueProcessor
{
private static readonly TelemetryClient telemetryClient = new TelemetryClient();

public void ProcessMessage(string message)
{
// Processing logic
telemetryClient.TrackEvent("MessageProcessed");
}
}

During testing, you observe that standard system metrics and dependency telemetry are successfully recorded in Application Insights, but the custom MessageProcessed events are missing.

Which of the following describes the root cause of this behavior?

  1. A
    The environment variable APPLICATIONINSIGHTS_CONNECTION_STRING is missing or not set in the Azure Container App environment settings.
  2. B
    The managed identity for the Azure Container App has not been configured with a Key Vault access policy allowing GET secret permissions.
  3. The static TelemetryClient is initialized using its parameterless constructor, which does not resolve the configured TelemetryConfiguration from the dependency injection container.Answer
  4. D
    The connection string defined in the App Configuration store contains an invalid Key Vault reference syntax, preventing configuration resolution.

Answer

The static TelemetryClient is initialized using its parameterless constructor, which does not resolve the configured TelemetryConfiguration from the dependency injection container.
The correct answer is correct because manually instantiating TelemetryClient via its parameterless constructor bypasses the dependency injection container. This results in the client using a default, empty TelemetryConfiguration without a connection string. Since the connection string is missing for this client instance, no custom events are tracked, while the system-registered services (which use the injected configuration) continue to function correctly.

Step-by-Step Solution

1
Identify the mechanism used to initialize TelemetryClient.
The code uses a parameterless constructor to manually create the TelemetryClient instance inside QueueProcessor.
To understand why custom events are missing, we must analyze how the client instance is configured.
2
Analyze the behavior of the parameterless TelemetryClient constructor in a DI-managed worker service.
The parameterless constructor does not automatically bind to the DI container's configured TelemetryConfiguration, defaulting to an unconfigured state with a missing connection string.
In modern .NET applications, dependencies should be injected; manually calling the constructor bypasses DI and leaves the instance without a valid destination for telemetry.
3
Compare this with the behavior of system telemetry.
System telemetry is registered via AddApplicationInsightsTelemetryWorkerService and correctly uses the DI container's configuration.
This explains why system metrics are successfully transmitted while custom metrics from the manual client are lost.

Key Concept

Dependency Injection and TelemetryClient configuration in .NET Core / .NET 8 Worker Services
Rate this question