ASP.NET Core  

Application Insights for ASP.NET Core

Introduction

Building a reliable ASP.NET Core application doesn't end with writing clean code and deploying it to production. Once your application is live, you need visibility into its performance, availability, errors, and user behavior. Without proper monitoring, identifying performance bottlenecks or diagnosing production issues can become difficult and time-consuming.

Azure Application Insights is an application performance monitoring (APM) service that helps developers collect telemetry, analyze application health, and troubleshoot issues in real time. It provides deep insights into requests, dependencies, exceptions, logs, and performance metrics, making it an essential tool for modern ASP.NET Core applications.

In this article, you'll learn how to integrate Application Insights into an ASP.NET Core application, explore its key features, and follow best practices for effective application monitoring.

What Is Azure Application Insights?

Azure Application Insights is a cloud-based monitoring service that collects telemetry from your application and presents it through interactive dashboards and analytics.

It automatically tracks various types of application data, including:

  • HTTP requests

  • Response times

  • Exceptions

  • Dependency calls

  • Availability

  • Performance metrics

  • Custom events

  • Trace logs

This information helps developers quickly identify and resolve production issues.

Why Use Application Insights?

Monitoring production applications manually is challenging, especially in cloud and distributed environments.

Application Insights provides several benefits:

  • Real-time monitoring

  • Automatic telemetry collection

  • Exception tracking

  • Performance analysis

  • Dependency monitoring

  • Availability testing

  • Custom telemetry

  • Interactive dashboards

These capabilities enable teams to detect and resolve problems before they affect users.

Create an Application Insights Resource

Before integrating Application Insights, create an Application Insights resource in Azure.

Once the resource is created, you'll receive a Connection String, which your application uses to send telemetry data.

Store this connection string securely using configuration files, environment variables, or Azure Key Vault.

Install the NuGet Package

Add the official Application Insights SDK to your project.

dotnet add package Microsoft.ApplicationInsights.AspNetCore

This package enables automatic telemetry collection for ASP.NET Core applications.

Configure Application Insights

Register Application Insights during application startup.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationInsightsTelemetry();

If your connection string is stored in configuration, the SDK automatically uses it.

Example in appsettings.json:

{
  "ApplicationInsights": {
    "ConnectionString": "YOUR_CONNECTION_STRING"
  }
}

This configuration allows the SDK to send telemetry to your Azure resource.

Automatic Request Tracking

Once configured, Application Insights automatically tracks incoming HTTP requests.

It records information such as:

  • Request URL

  • Response time

  • HTTP status code

  • Success or failure

  • Request duration

This data helps identify slow endpoints and monitor API performance.

Monitor Dependencies

Most applications interact with external resources such as databases, REST APIs, or messaging services.

Application Insights automatically tracks many dependency calls, including:

  • SQL Server

  • Azure SQL Database

  • HTTP services

  • Azure Storage

  • Azure Service Bus

Dependency tracking helps identify slow or failing external services that may impact application performance.

Track Exceptions

Unhandled exceptions are automatically captured by Application Insights.

You can also log exceptions manually when additional context is needed.

using Microsoft.ApplicationInsights;

public class ProductService
{
    private readonly TelemetryClient _telemetry;

    public ProductService(TelemetryClient telemetry)
    {
        _telemetry = telemetry;
    }

    public void LogError(Exception ex)
    {
        _telemetry.TrackException(ex);
    }
}

Capturing exceptions with context makes troubleshooting much easier.

Track Custom Events

In addition to automatic telemetry, you can record business-specific events.

telemetryClient.TrackEvent("OrderPlaced");

Custom events can represent actions such as:

  • User registration

  • Order placement

  • File uploads

  • Payment completion

  • Report generation

These events provide valuable insights into application usage.

Track Custom Metrics

Application Insights also supports custom metrics.

telemetryClient.TrackMetric(
    "ActiveUsers",
    125);

Custom metrics allow you to monitor application-specific values that are not collected automatically.

Examples include:

  • Active users

  • Orders processed

  • Cache hit ratio

  • Queue length

Logging with ILogger

Application Insights integrates seamlessly with ASP.NET Core logging.

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(
        ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Home page visited.");

        return View();
    }
}

Logs generated through ILogger can be viewed alongside other telemetry, making it easier to correlate application events.

Monitor Application Performance

Application Insights provides detailed performance data, including:

  • Request duration

  • Failed requests

  • Server response time

  • Database performance

  • External dependency latency

  • CPU usage

  • Memory consumption

Analyzing these metrics helps identify performance bottlenecks and optimize resource usage.

Availability Monitoring

Application Insights supports availability tests that periodically verify whether your application is accessible.

Common test types include:

  • URL ping tests

  • Multi-region availability checks

  • Response validation

These tests help detect outages before users report them.

Best Practices

To maximize the value of Application Insights:

  • Enable telemetry early in the project.

  • Use structured logging with ILogger.

  • Track important business events.

  • Create custom metrics for key application indicators.

  • Configure alerts for critical failures.

  • Review performance dashboards regularly.

  • Protect sensitive data by avoiding confidential information in telemetry.

  • Monitor dependency performance and failure rates.

  • Keep the SDK updated to benefit from the latest improvements.

Following these practices helps maintain a healthy and observable application.

Common Mistakes to Avoid

Developers sometimes reduce the effectiveness of Application Insights through poor implementation choices.

Avoid these common mistakes:

  • Logging sensitive personal or financial data.

  • Ignoring failed dependency calls.

  • Collecting excessive custom telemetry without a clear purpose.

  • Failing to configure alerts for critical exceptions.

  • Neglecting regular telemetry reviews.

  • Assuming automatic telemetry covers every business scenario.

Combining automatic telemetry with carefully chosen custom events provides the best monitoring experience.

Application Insights vs Traditional Logging

The following comparison highlights the differences between basic logging and Application Insights.

FeatureTraditional LoggingApplication Insights
Request TrackingLimitedYes
Exception TrackingBasicAdvanced
Performance MonitoringLimitedYes
Dependency TrackingNoYes
Real-Time DashboardsNoYes
Custom MetricsLimitedYes

Application Insights extends traditional logging by providing rich telemetry and analytics.

Conclusion

Azure Application Insights is a powerful monitoring solution that helps ASP.NET Core developers understand how their applications perform in real-world environments. With automatic telemetry collection, exception tracking, dependency monitoring, custom events, and performance analytics, it provides the visibility needed to maintain reliable and high-performing applications.

By integrating Application Insights early in your development process and following best practices for telemetry collection, logging, and alerting, you can detect issues faster, improve application performance, and deliver a better experience for your users.