.NET Application Insights
Introduction
Application Insights is a powerful application performance management (APM) service provided by Microsoft Azure. It allows developers to monitor live .NET applications, automatically detect performance anomalies, and gain insights into how users interact with your applications. Whether you're building web applications, APIs, or background services, Application Insights provides valuable telemetry data that helps you understand your application's behavior in production environments.
In this guide, you'll learn how to integrate Application Insights into your .NET applications, understand the different types of telemetry it collects, and see how to use this data to improve your application's performance and reliability.
Why Use Application Insights?
Before diving into implementation details, let's understand why Application Insights is valuable for .NET developers:
- Real-time monitoring - Track application performance and usage in production
- Automatic detection of issues - Get alerts for unusual patterns or performance degradation
- Rich analytics - Analyze telemetry data using powerful query language
- End-to-end transaction monitoring - Follow requests from client to backend services
- User behavior analytics - Understand how users interact with your application
- Integration with Azure DevOps - Create work items from detected issues
Setting Up Application Insights
Step 1: Create an Application Insights Resource
First, you need to create an Application Insights resource in Azure:
- Sign in to the Azure Portal
- Create a new resource by clicking "Create a resource"
- Search for "Application Insights" and select it
- Fill in the required details like subscription, resource group, name, and region
- Click "Review + create" and then "Create"
Once created, you'll need the Instrumentation Key or Connection String from the resource overview page.
Step 2: Add Application Insights to Your .NET Application
For .NET Core and .NET 5+ Applications
For ASP.NET Core applications, the easiest way to add Application Insights is via the built-in service:
- Install the required NuGet package:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
- Add Application Insights to the service collection in your
Program.cs
file:
var builder = WebApplication.CreateBuilder(args);
// Add Application Insights services
builder.Services.AddApplicationInsightsTelemetry();
// Other services...
builder.Services.AddControllers();
var app = builder.Build();
// Configure middleware
app.Run();
- Configure the connection string in your
appsettings.json
:
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=your-instrumentation-key;IngestionEndpoint=https://your-region.in.applicationinsights.azure.com/;LiveEndpoint=https://your-region.livediagnostics.monitor.azure.com/"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
For .NET Framework Applications
For traditional ASP.NET applications:
- Install the required NuGet packages:
Install-Package Microsoft.ApplicationInsights.AspNetCore
Install-Package Microsoft.ApplicationInsights.DependencyCollector
Install-Package Microsoft.ApplicationInsights.PerfCounterCollector
- Add the instrumentation key to your
Web.config
file:
<configuration>
<appSettings>
<add key="ApplicationInsights:InstrumentationKey" value="your-instrumentation-key" />
</appSettings>
</configuration>
- Initialize Application Insights in
Global.asax.cs
:
using Microsoft.ApplicationInsights.Extensibility;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Set up Application Insights
TelemetryConfiguration.Active.InstrumentationKey =
ConfigurationManager.AppSettings["ApplicationInsights:InstrumentationKey"];
// Rest of your startup code
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
// Other configuration...
}
}
Types of Telemetry Collected
Application Insights automatically collects several types of telemetry:
- Request data - HTTP requests to your web application
- Dependency calls - Calls to databases, external APIs, or other services
- Exceptions - Unhandled and handled exceptions
- Page views and user behavior (for web applications)
- Performance counters - CPU, memory usage, etc.
- Custom events and metrics - Data you define and collect
Logging Custom Telemetry
Tracking Custom Events
You can track custom events to understand user behavior or application flow:
// Inject the telemetry client
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly TelemetryClient _telemetryClient;
public HomeController(ILogger<HomeController> logger, TelemetryClient telemetryClient)
{
_logger = logger;
_telemetryClient = telemetryClient;
}
public IActionResult Index()
{
// Track a custom event
_telemetryClient.TrackEvent("HomePageVisited");
// Track an event with custom properties
_telemetryClient.TrackEvent("UserAction", new Dictionary<string, string>
{
{ "UserType", "Registered" },
{ "Feature", "Dashboard" }
});
return View();
}
}
Tracking Custom Metrics
Custom metrics help you measure specific aspects of your application:
public void ProcessOrder(Order order)
{
var stopwatch = Stopwatch.StartNew();
// Process the order...
stopwatch.Stop();
// Track processing time
_telemetryClient.TrackMetric("OrderProcessingTime", stopwatch.ElapsedMilliseconds);
// Track order value
_telemetryClient.TrackMetric("OrderValue", order.TotalAmount);
}
Tracking Dependencies
You can manually track dependencies that aren't automatically captured:
public async Task<ExternalData> CallExternalServiceAsync()
{
// Start the operation
var operation = _telemetryClient.StartOperation<DependencyTelemetry>("ExternalService");
operation.Telemetry.Type = "HTTP";
operation.Telemetry.Target = "api.externalservice.com";
try
{
// Make the external call
var result = await _httpClient.GetFromJsonAsync<ExternalData>("https://api.externalservice.com/data");
// Mark as successful
operation.Telemetry.Success = true;
return result;
}
catch (Exception ex)
{
// Track the exception
operation.Telemetry.Success = false;
_telemetryClient.TrackException(ex);
throw;
}
finally
{
// Complete the operation
_telemetryClient.StopOperation(operation);
}
}
Viewing and Analyzing Telemetry Data
After deploying your application with Application Insights configured, you can view the collected telemetry data in the Azure Portal:
- Navigate to your Application Insights resource
- The overview page shows key metrics like request rates, failure rates, and response times
- Use the "Application Map" to visualize dependencies between components
- Explore "Performance" to identify slow requests or dependencies
- Check "Failures" to find exceptions and failed requests
- Use "Metrics" to create custom charts and dashboards
Using Kusto Query Language (KQL)
Application Insights uses Kusto Query Language (KQL) for advanced analysis. Here are some useful queries:
// Find the slowest requests
requests
| where timestamp > ago(24h)
| order by duration desc
| take 10
// Count exceptions by type
exceptions
| where timestamp > ago(24h)
| summarize count() by type
| order by count_ desc
// User retention analysis
customEvents
| where name == "UserLogin"
| summarize UserCount = dcount(user_Id) by bin(timestamp, 1d)
| render timechart
Real-world Example: Monitoring a Web API
Let's look at a complete example of monitoring a .NET Web API:
- Create a new Web API project:
dotnet new webapi -n MonitoredApi
cd MonitoredApi
- Add Application Insights:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
- Update
Program.cs
to include Application Insights:
var builder = WebApplication.CreateBuilder(args);
// Add Application Insights
builder.Services.AddApplicationInsightsTelemetry();
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
- Add a custom controller with telemetry tracking:
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace MonitoredApi.Controllers;
[ApiController]
[Route("[controller]")]
public class OrdersController : ControllerBase
{
private readonly ILogger<OrdersController> _logger;
private readonly TelemetryClient _telemetryClient;
public OrdersController(ILogger<OrdersController> logger, TelemetryClient telemetryClient)
{
_logger = logger;
_telemetryClient = telemetryClient;
}
[HttpGet]
public IActionResult Get()
{
// Track a custom event
_telemetryClient.TrackEvent("OrdersListed");
return Ok(new[] { new { Id = 1, Total = 99.99 }, new { Id = 2, Total = 149.99 } });
}
[HttpPost]
public IActionResult Create([FromBody] OrderModel order)
{
var stopwatch = Stopwatch.StartNew();
try
{
// Simulate order processing
if (order.Total <= 0)
{
throw new ArgumentException("Order total must be positive");
}
// Track the business metric
_telemetryClient.TrackMetric("OrderValue", order.Total);
// Simulate external API call
SimulateExternalApiCall();
stopwatch.Stop();
// Track processing time
_telemetryClient.TrackMetric("OrderProcessingTime", stopwatch.ElapsedMilliseconds);
return CreatedAtAction(nameof(Get), new { id = 123 });
}
catch (Exception ex)
{
// Track exception with custom properties
_telemetryClient.TrackException(ex, new Dictionary<string, string>
{
{ "OrderId", order.Id.ToString() },
{ "OrderTotal", order.Total.ToString() }
});
return BadRequest("Invalid order");
}
}
private void SimulateExternalApiCall()
{
var operation = _telemetryClient.StartOperation<DependencyTelemetry>("PaymentProcessor");
operation.Telemetry.Type = "HTTP";
operation.Telemetry.Target = "api.payments.com";
try
{
// Simulate processing time
Thread.Sleep(200);
// Randomly simulate failures (10% chance)
if (new Random().Next(10) == 0)
{
throw new Exception("Payment processor error");
}
operation.Telemetry.Success = true;
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
_telemetryClient.TrackException(ex);
throw;
}
finally
{
_telemetryClient.StopOperation(operation);
}
}
}
public class OrderModel
{
public int Id { get; set; }
public double Total { get; set; }
}
- Configure in
appsettings.json
:
{
"ApplicationInsights": {
"ConnectionString": "YOUR_CONNECTION_STRING_HERE"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},
"AllowedHosts": "*"
}
After deploying this application, you'll be able to:
- Monitor request rates, response times, and failure rates
- Track custom business metrics like order value and processing time
- Identify issues with the payment processor dependency
- Analyze exceptions with contextual information
Best Practices
- Don't over-instrument - Focus on business-critical events and metrics
- Use sampling - For high-volume applications, enable sampling to reduce costs
- Add context - Include relevant properties with events and metrics
- Set up alerts - Configure alerts for important thresholds and anomalies
- Use operation context - Correlate related events using operation IDs
- Separate development and production - Use different instrumentation keys for different environments
- Be mindful of PII - Don't track personally identifiable information
Common Issues and Solutions
High Data Volume and Costs
If you're collecting too much telemetry:
// Configure sampling in Program.cs
builder.Services.AddApplicationInsightsTelemetry(options => {
options.EnableAdaptiveSampling = true;
options.SamplingPercentage = 10; // Sample 10% of requests
});
Missing Dependency Data
Make sure you've added the dependency collector:
dotnet add package Microsoft.ApplicationInsights.DependencyCollector
Custom Configuration
To provide more control over configuration:
builder.Services.AddApplicationInsightsTelemetry(options => {
options.ConnectionString = builder.Configuration["CustomAIConnectionString"];
options.EnableHeartbeat = true;
options.EnableDependencyTrackingTelemetryModule = true;
});
Summary
Application Insights provides powerful monitoring capabilities for .NET applications, giving you visibility into your application's performance and behavior in production. By collecting and analyzing telemetry data, you can:
- Detect and diagnose exceptions and performance issues
- Understand user behavior and usage patterns
- Monitor dependencies and external services
- Track custom business metrics
- Receive alerts when issues occur
The integration with .NET applications is straightforward, especially with modern .NET Core and .NET 5+ applications, where most of the configuration is handled automatically. By following the guidelines in this tutorial, you can enhance your application with comprehensive monitoring that helps you build more reliable and performant software.
Additional Resources
- Official Application Insights Documentation
- Application Insights SDK for ASP.NET Core
- Kusto Query Language (KQL) Documentation
- Application Insights Pricing
Exercises
- Add Application Insights to an existing .NET application and monitor its basic metrics.
- Create custom events to track a user workflow through your application.
- Set up custom metrics to measure business-specific performance indicators.
- Configure an alert that notifies you when the response time exceeds a threshold.
- Use the Kusto Query Language to analyze telemetry data and create a custom report.
- Implement correlation IDs to track a request across multiple services or components.
By completing these exercises, you'll gain hands-on experience with Application Insights and be well-equipped to monitor your .NET applications in production.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)