Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing a .NET 8 background worker service that processes queue messages and communicates with an external database. You need to manually track the database call as a dependency using the Application Insights SDK, and ensure any thrown exceptions are logged to Application Insights.

Complete the code by filling in the blanks with the correct TelemetryClient method names.

Answer:using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using System;
using System.Threading.Tasks;

public class QueueProcessor
{
private readonly TelemetryClient _telemetryClient;
private readonly IExternalService _externalService;

public QueueProcessor(TelemetryClient telemetryClient, IExternalService externalService)
{
_telemetryClient = telemetryClient;
_externalService = externalService;
}

public async Task ProcessJobAsync(string jobId)
{
// Start and correlate a dependency tracking operation
using (var operation = _telemetryClient.【StartOperation】<DependencyTelemetry>("DatabaseCall"))
{
try
{
await _externalService.ExecuteAsync(jobId);
operation.Telemetry.Success = true;
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
_telemetryClient.【TrackException】(ex);
throw;
}
}
}
}

Answer

The first blank must be 'StartOperation' to begin a correlated telemetry tracking operation, and the second blank must be 'TrackException' to record the exception details in Application Insights.
The correct method for starting a scoped operation telemetry flow is `StartOperation`, which integrates seamlessly with C#'s `using` pattern to capture telemetry duration. The correct method for recording raw application exceptions is `TrackException` to capture the complete error signature.

Step-by-Step Solution

1
Identify the telemetry tracking pattern designed to measure operation duration and context propagation using C# using-blocks.
The `StartOperation` extension method of `TelemetryClient` starts a timed operation scope and returns an `IOperationHolder<T>` instance.
Using `StartOperation` automatically sets start time, tracks duration upon disposal, and correlates sub-operations.
2
Determine the correct telemetry API method to log exceptions with full call stack details.
The `TrackException` method accepts an `Exception` object to send to the Application Insights exception log store.
Using `TrackException` maps the caught error to Azure Monitor exception tables, ensuring diagnostic details are preserved.

Key Concept

Manual dependency tracking and exception instrumentation with Application Insights SDK
Rate this question