You are developing an ASP.NET Core Web API hosted on Azure App Service. You must implement a custom telemetry processor named DependencyFilterProcessor to filter out successful SQL dependency telemetry before it is sent to Application Insights. You also need to register this telemetry processor in the dependency injection container.
How should you complete the code segments for the processor implementation and service registration?
Cevap:csharp
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
// Register the custom telemetry processor
builder.Services.【AddApplicationInsightsTelemetryProcessor】<DependencyFilterProcessor>();
// DependencyFilterProcessor.cs
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
public class DependencyFilterProcessor : 【ITelemetryProcessor】
{
private readonly 【ITelemetryProcessor】 _next;
public DependencyFilterProcessor(【ITelemetryProcessor】 next)
{
_next = next;
}
public void Process(ITelemetry item)
{
// Filtering logic goes here
_next.Process(item);
}
}
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
// Register the custom telemetry processor
builder.Services.【AddApplicationInsightsTelemetryProcessor】<DependencyFilterProcessor>();
// DependencyFilterProcessor.cs
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
public class DependencyFilterProcessor : 【ITelemetryProcessor】
{
private readonly 【ITelemetryProcessor】 _next;
public DependencyFilterProcessor(【ITelemetryProcessor】 next)
{
_next = next;
}
public void Process(ITelemetry item)
{
// Filtering logic goes here
_next.Process(item);
}
}
Cevap
To configure a custom telemetry processor, register it in the dependency injection container using the `AddApplicationInsightsTelemetryProcessor<DependencyFilterProcessor>()` extension method. The processor class and its constructor argument must implement the `ITelemetryProcessor` interface.
The correct implementation utilizes `AddApplicationInsightsTelemetryProcessor` to register the processor, which implements `ITelemetryProcessor` and receives the next `ITelemetryProcessor` in its constructor to continue the pipeline execution.
Adım Adım Çözüm
Anahtar Kavram
Custom Telemetry Processors in Application Insights