Monitoring .NET applications is essential for ensuring performance, reliability, scalability, and user experience in production environments. Application Insights is an application performance management (APM) service that enables developers and DevOps teams to collect telemetry, analyze performance metrics, track exceptions, monitor dependencies, and gain deep observability into distributed systems.
In modern ASP.NET Core applications, microservices, cloud-native systems, and containerized workloads, Application Insights provides real-time diagnostics, distributed tracing, live metrics, and intelligent anomaly detection. This article explains how to implement Application Insights in .NET applications, configure telemetry, track custom events, monitor dependencies, analyze logs, and apply best practices.
What Is Application Insights?
Application Insights is a monitoring and observability platform that collects telemetry data from applications, including:
Request rates and response times
Failure rates and exceptions
Dependency calls (SQL, HTTP, external APIs)
Performance counters
Custom events and metrics
Distributed traces
It supports ASP.NET Core, worker services, background jobs, Azure Functions, and containerized .NET applications.
Why Monitoring Is Critical for .NET Applications
Without proper monitoring:
Performance bottlenecks remain hidden
Production errors go undetected
Root cause analysis becomes difficult
SLA violations increase
User experience degrades
With Application Insights:
Real-time telemetry is available
Alerts can be configured
Failures are traceable across services
Query-based diagnostics are possible
Trends and anomalies are detectable
Step-by-Step Implementation in ASP.NET Core
Step 1: Install Application Insights Package
Install the required NuGet package:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
Step 2: Configure Application Insights in Program.cs
Register Application Insights services:
builder.Services.AddApplicationInsightsTelemetry();
Provide the connection string in appsettings.json:
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=YOUR_KEY;IngestionEndpoint=YOUR_ENDPOINT"
}
}
Alternatively, configure using environment variables in production.
Step 3: Automatic Telemetry Collection
Once configured, Application Insights automatically tracks:
Incoming HTTP requests
Response times
Failed requests
Unhandled exceptions
SQL dependency calls
Outbound HTTP calls
Example ASP.NET Core controller:
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok(new { Message = "Monitoring Enabled" });
}
}
Requests to this endpoint are automatically tracked.
Step 4: Track Custom Events
To log custom business events, inject TelemetryClient.
using Microsoft.ApplicationInsights;
public class OrderService
{
private readonly TelemetryClient _telemetryClient;
public OrderService(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
}
public void PlaceOrder(string orderId)
{
_telemetryClient.TrackEvent("OrderPlaced", new Dictionary<string, string>
{
{ "OrderId", orderId }
});
}
}
This enables tracking domain-specific events.
Step 5: Track Custom Metrics
_telemetryClient.TrackMetric("CartValue", 250);
Custom metrics help monitor KPIs such as revenue, user activity, or transaction count.
Step 6: Track Exceptions Manually
Unhandled exceptions are captured automatically, but manual tracking provides better context.
try
{
ProcessPayment();
}
catch (Exception ex)
{
_telemetryClient.TrackException(ex);
throw;
}
Step 7: Enable Logging Integration
Application Insights integrates with ASP.NET Core logging.
In Program.cs:
builder.Logging.AddApplicationInsights();
Then use ILogger normally:
private readonly ILogger<ProductService> _logger;
_logger.LogInformation("Product retrieved successfully.");
_logger.LogError("Product not found.");
Logs are searchable in the monitoring dashboard.
Join the conversation! Your thoughts help the community grow.