ASP.NET Web API Using Application Insights for Monitoring Step by step Implementation and Top 10 Questions and Answers
 Last Update: April 01, 2025      9 mins read      Difficulty-Level: beginner

Certainly! Let's walk through the process of setting up ASP.NET Web API with Application Insights for effective monitoring from the ground up. We'll break down the process step-by-step into detailed explanations to ensure clarity, even for beginners.

Step 1: Understand the Components

Before diving into the implementation, it's crucial to understand the components involved:

  1. ASP.NET Web API: This is a framework for building HTTP services that reach a broad range of clients, including browsers and mobile devices.
  2. Application Insights: A versatile application performance management (APM) service provided by Microsoft Azure that helps developers monitor their live applications, diagnose issues, and understand user behavior.

Step 2: Set Up Your ASP.NET Web API Project

  1. Create a New Project:

    • Open Visual Studio.
    • Navigate to File > New > Project.
    • Select ASP.NET Web Application (.NET Core).
    • Name and choose a location for your project.
    • Click Create.
  2. Choose a Template:

    • In the new ASP.NET Core Web Application window, select API from the project templates.
    • Make sure to check the box labeled Enable Docker Support if you plan to deploy your application using Docker.
    • Click Create.

Step 3: Set Up Application Insights

  1. Add NuGet Packages:

    • If you're creating the project from scratch and don't see Application Insights as an option in the wizard, you can manually add it.
    • Right-click on your project in the Solution Explorer.
    • Select Manage NuGet Packages.
    • Search for Microsoft.ApplicationInsights.AspNetCore.
    • Click Install.
  2. Add Application Insights Service:

    • Open Program.cs for ASP.NET Core 6.0 and later versions or Startup.cs for earlier versions.
    • For ASP.NET Core 6.0, you will find the var builder = WebApplication.CreateBuilder(args); line.
    • Add the Application Insights service with this line of code:
      builder.Services.AddApplicationInsightsTelemetry();
      
    • For earlier versions (like ASP.NET Core 3.1 and .NET 5), modify the ConfigureServices method:
      services.AddApplicationInsightsTelemetry();
      
  3. Instrumentation Key:

    • If you're connecting to an existing Azure Application Insights instance, you will need the instrumentation key.
    • Find the appsettings.json file.
    • Add the instrumentation key under the ApplicationInsights section:
      {
        "ApplicationInsights": {
          "InstrumentationKey": "your_instrumentation_key_here"
        }
      }
      
    • If you are creating a new Application Insights instance, you can set it up through the Azure portal and get the instrumentation key from there.
  4. Enable Telemetry Collection:

    • Telemetry modules are automatically added when you call AddApplicationInsightsTelemetry().
    • You can customize telemetry modules if necessary by modifying the ConfigureServices method.

Step 4: Configure Application Insights

  1. Custom Telemetry Initializers:

    • You can add custom telemetry initializers to enrich the default telemetry data captured by Application Insights.
    • Create a new class that implements ITelemetryInitializer.
    • Add custom properties or modify existing ones.
    • Register your custom telemetry initializer in the ConfigureServices method:
      services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
      
  2. Custom Telemetry Processors:

    • Telemetry processors provide more advanced telemetry filtering.
    • Create a new class that implements ITelemetryProcessor.
    • Implement filtering logic in the Process method.
    • Register your custom telemetry processor in the ConfigureServices method:
      services.AddSingleton<ITelemetryProcessor, MyCustomTelemetryProcessor>();
      

Step 5: Utilize TelemetryClient for Custom Telemetry Events

  1. Add a TelemetryClient:

    • Inject TelemetryClient into your controllers, services, or anywhere you need to send custom telemetry:
      private readonly TelemetryClient _telemetryClient;
      
      public WeatherForecastController(TelemetryClient telemetryClient)
      {
          _telemetryClient = telemetryClient;
      }
      
  2. Track Events:

    • Use the TelemetryClient to track events, metrics, exceptions, and more.
    • Example of tracking an event:
      _telemetryClient.TrackEvent("WeatherForecastRequested", properties: new Dictionary<string, string> { { "UserId", userId } });
      
  3. Track Metrics:

    • Track metrics to gather quantitative data about your application's performance.
    • Example of tracking a metric:
      _telemetryClient.TrackMetric("ApiRequestDuration", timeTakenInSeconds);
      

Step 6: Configure Application Insights SDK for Advanced Scenarios

  1. Telemetry Configuration:

    • Modify the TelemetryConfiguration to customize telemetry behavior.
    • Example of setting a custom endpoint:
      builder.Services.Configure<TelemetryConfiguration>(config =>
      {
          config.TelemetryChannel.EndpointAddress = "https://customendpoint";
      });
      
  2. Telemetry Correlation:

    • Application Insights provides automatic correlation out of the box.
    • Ensure that your application respects headers like Request-Id and X-B3-TraceId to maintain correct correlation across services.

Step 7: Deploy and Monitor

  1. Deploy Your Application:

    • Deploy your ASP.NET Web API to your desired environment (Azure App Service, Azure Kubernetes Service, Docker, etc.).
    • Ensure that the instrumentation key is correctly set up and the application has internet access to send telemetry data to Application Insights.
  2. Access Application Insights:

    • Navigate to the Azure portal.
    • Go to your Application Insights instance.
    • Use the dashboard, analytics, and logs features to monitor your application's performance and behavior.

Step 8: Analyze and Optimize

  1. Performance Analysis:

    • Use the performance tab to identify slow requests and understand call paths.
    • Utilize dependencies and exceptions tabs to pinpoint failures.
  2. User Behavior:

    • Track user behavior with page views, custom events, and exceptions.
    • Customize your instrumentation to capture the key metrics you need for your application.
  3. Alerts and Automation:

    • Set up alerts for critical metrics and exceptions.
    • Automate responses using Azure Functions, Azure Logic Apps, or other automation tools.

Conclusion

Setting up ASP.NET Web API with Application Insights involves integrating the SDK, configuring options, tracking telemetry, and using the insights to monitor and optimize your application's performance. By following the steps above, you can ensure that your application is well-equipped to handle the challenges it may face and provide insights that lead to better performance and user experience. Start by building a basic setup and gradually explore the advanced features of Application Insights as your monitoring needs grow. Happy coding!