Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing a .NET 8 console application that will run as a WebJob on a Linux Azure App Service. The application must track custom operational metrics by using the Application Insights SDK.

You write the following code to initialize the telemetry tracking:

csharp
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

class Program
{
static void Main(string[] args)
{
var config = TelemetryConfiguration.CreateDefault();
var client = new TelemetryClient(config);

client.TrackEvent("WebJobStarted");
client.Flush();
}
}

When you run the WebJob, the application executes without throwing any exceptions, but no custom events are visible in your Application Insights resource.

Which of the following changes must you make to ensure that the telemetry data is sent successfully to Azure Monitor?

  1. A
    Instantiate the TelemetryConfiguration using the default constructor and assign your Application Insights instrumentation key to the InstrumentationKey property.
  2. Set the ConnectionString property of the TelemetryConfiguration instance to your Application Insights connection string before instantiating the TelemetryClient.Answer
  3. C
    Configure a Key Vault access policy that grants the App Service's system-assigned managed identity GET permissions for the Application Insights secret.
  4. D
    Define an environment variable on the App Service with the Key Vault reference syntax without the secret URI to automatically bind the connection string.

Answer

Set the ConnectionString property of the TelemetryConfiguration instance to your Application Insights connection string before instantiating the TelemetryClient.
Setting the connection string directly on the TelemetryConfiguration instance ensures that the TelemetryClient knows where to send the telemetry. In modern Application Insights SDKs, setting the connection string is mandatory for routing telemetry data, and manually initializing a TelemetryConfiguration without setting this property results in telemetry being silently dropped.

Step-by-Step Solution

1
Identify how the TelemetryClient is initialized in the code snippet.
The code creates a TelemetryConfiguration using TelemetryConfiguration.CreateDefault() and passes it to the TelemetryClient constructor.
Analyzing the initialization flow is necessary to see where telemetry routing configuration is missing.
2
Determine why no telemetry is being sent to Azure Monitor.
The TelemetryConfiguration instance does not have its ConnectionString property set, which means the client has no endpoint to send the telemetry to.
Establishing the root cause of the silent telemetry failure.
3
Configure the connection string in the application code.
Assign the target Application Insights connection string to the ConnectionString property of the config object before creating the TelemetryClient instance.
This supplies the required destination endpoint for the Application Insights SDK to successfully transmit custom event telemetry.

Key Concept

Manually configuring Application Insights TelemetryConfiguration with a Connection String
Rate this question