ASP.NET Core  

Implementing Application Insights in a .NET Core Web API: A Complete Guide

Introduction

Application monitoring is a critical part of modern software development. Without proper telemetry, diagnosing performance issues and tracking failures becomes difficult. Microsoft provides Application Insights, a powerful monitoring service within Microsoft Azure that helps developers collect, analyze, and act on telemetry data from their applications.

This article provides a step-by-step guide to integrating Application Insights into a .NET Core Web API, including setup, configuration, deployment, and verification.

Prerequisites

Before proceeding, ensure you have:

  • An active Azure subscription

  • A .NET Core Web API project

  • Access to Azure Portal

Step 1: Create an Application Insights Resource in Azure Portal

Sign in to Azure Portal

  1. Navigate to the Azure Portal.

  2. Log in using your Azure credentials.

Create the Resource

  1. Click Create a resource.

  2. Search for Application Insights.

  3. Select it and click Create.

Configure the Resource

Provide the following details:

  • Resource Group – Select an existing one or create a new resource group.

  • Name – Enter a unique name for your Application Insights instance.

  • Region – Choose the region where your application is hosted.

  • Resource Mode – Select:

    • Classic, or

    • Workspace-based (recommended for integration with Log Analytics).

Click Review + Create, validate the configuration, and then click Create.

Once deployment is complete, the resource is ready for integration.

Step 2: Install Application Insights SDK

To enable telemetry collection in your .NET Core Web API, install the required NuGet package:

Microsoft.ApplicationInsights.AspNetCore

You can install it using:

  • NuGet Package Manager

  • Package Manager Console

  • .NET CLI:

dotnet add package Microsoft.ApplicationInsights.AspNetCore

After installation, your application is ready to connect with Azure Application Insights.

Step 3: Configure Application Insights in the Application

Retrieve the Connection String

  1. Open your Application Insights resource in Azure Portal.

  2. Go to the Overview section.

  3. Copy the Connection String (recommended) or Instrumentation Key.

Update appsettings.json

Add the following configuration:

{
  "ApplicationInsights": {
    "ConnectionString": "Your_Connection_String_Here"
  }
}

Configure Program.cs

In .NET 6 or later, add:

builder.Services.AddApplicationInsightsTelemetry();

This enables automatic telemetry collection including:

  • HTTP request tracking

  • Exception tracking

  • Dependency tracking (SQL, HTTP calls)

  • Performance metrics

Step 4: Deploy and Verify Telemetry

Deploy the Application

Deploy your Web API to:

  • Azure App Service

  • Virtual Machine

  • Any supported hosting environment

Verify Data in Azure Portal

After deployment and a few minutes of activity:

  1. Navigate to your Application Insights resource.

  2. Explore the following sections:

  • Live Metrics – Real-time monitoring

  • Transaction Search – Detailed request and log data

  • Failures – Exception analysis

  • Performance – Response time and dependency tracking

You should start seeing:

  • Request count

  • Response times

  • Failure rates

  • Server-side exceptions

Implementing Logging with ILogger

To capture detailed logs, use ILogger in your controller or service.

Example:

private readonly ILogger<HomeController> _logger;

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

public IActionResult Index()
{
    _logger.LogInformation("Information log example.");
    _logger.LogError("Error log example.");
    return Ok();
}

These logs can be viewed under:

Application Insights → Transaction Search

Output and Observations

Once the application runs successfully:

  • Requests are automatically tracked.

  • Exceptions are logged with stack traces.

  • Dependencies such as SQL calls are monitored.

  • Custom logs appear in the telemetry search section.

This provides a centralized monitoring solution for your application.

Advantages of Using Application Insights

  • Real-time monitoring

  • Automatic dependency tracking

  • Centralized logging

  • Performance diagnostics

  • Failure analysis

  • Integration with Log Analytics

Conclusion

Integrating Application Insights into a .NET Core Web API application is a straightforward yet powerful way to implement monitoring and diagnostics. With minimal configuration, developers can gain deep visibility into application behavior, performance bottlenecks, and runtime exceptions.

For production-grade applications, implementing monitoring is not optional—it is essential. By following the steps outlined in this article, you can ensure that your application is fully equipped with robust telemetry and logging capabilities.