How to Configure Application Insights Telemetry in ASP.NET Core?

Asked 4 hours ago Updated 3 hours ago 16 views

0

Azure Application Insights provides cloud-scale application performance monitoring (APM) for web applications, offering automated dependency tracking, request logging, and performance diagnostics.

Installing Application Insights SDK

Add the Application Insights NuGet package to your ASP.NET Core project:

  • Microsoft.ApplicationInsights.AspNetCore

Enabling Telemetry Service

Register Application Insights telemetry in your Program.cs file by supplying your Connection String:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationInsightsTelemetry(options =>
{
    options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
});

var app = builder.Build();

Injecting Custom Telemetry

Track custom events or metrics using TelemetryClient inside your controllers or backend services:

using Microsoft.ApplicationInsights;

[ApiController]
[Route("api/[controller]")]
public class MetricsController : ControllerBase
{
    private readonly TelemetryClient _telemetryClient;

    public MetricsController(TelemetryClient telemetryClient)
    {
        _telemetryClient = telemetryClient;
    }

    [HttpPost("checkout")]
    public IActionResult Checkout()
    {
        _telemetryClient.TrackEvent("UserCheckoutCompleted");
        return Ok();
    }
}

0 Answers


Write Your Answer