Setting Up Azure Application Insights in Azure Portal and Configuring it in a .NET Core Web API Application | Step-by-Step Guide

Introduction:

Azure Application Insights is a powerful tool that allows you to monitor and gain insights into your applications, including performance, availability, and usage. Integrating Application Insights into your .NET Core Web API application can provide valuable telemetry data and help you identify and diagnose issues quickly. In this step-by-step guide, we will walk you through the process of setting up Azure Application Insights in the Azure Portal and configuring it in your .NET Core Web API application. By the end of this guide, you will have a comprehensive understanding of how to leverage Application Insights for effective monitoring and diagnostics in your Web API projects.

Table of Contents:

  1. Prerequisites
  2. Creating an Application Insights resource in the Azure Portal
  3. Configuring Application Insights in your .NET Core Web API application
    • Installing the necessary packages
    • Adding Application Insights to the Project Configuration
    • Instrumenting your code for telemetry
  4. Verifying Application Insights integration
  5. Exploring Application Insights features
    • Performance monitoring
    • Availability and failure analysis
    • Request tracking and dependency monitoring
    • Custom telemetry and metrics
  6. Best practices for using Application Insights in your Web API application
  7. Conclusion

Demo

Prerequisites:

Before getting started, make sure you have the following:

  • An Azure subscription
  • Visual Studio or any preferred code editor installed
  • Basic knowledge of .NET Core and Web API development

Creating an Application Insights resource in the Azure Portal:

To create an Application Insights resource in the Azure Portal, follow these steps:

  1. Log in to the Azure Portal (https://portal.azure.com) using your Azure account.
  2. Click on the “+ Create a resource” button on the top left corner of the portal.
  3. In the search bar, type “Application Insights” and select “Application Insights” from the search results.
  4. Click on the “Create” button to start the creation process.
  5. Fill in the required details for your Application Insights resource, such as subscription, resource group, and name.
  6. Choose the desired Application Type, such as “Web” for a Web API application.
  7. Select the appropriate Application Insights pricing tier based on your requirements.
  8. Click on the “Review + Create” button, review the settings, and then click on “Create” to create the Application Insights resource.
  9. Once the resource is created, navigate to the resource page, and note down the “Instrumentation Key” as we will need it later for configuration.

Configuring Application Insights in your .NET Core Web API application:

To configure Application Insights in your .NET Core Web API application, follow these steps:

  1. Open your .NET Core Web API project in Visual Studio or your preferred code editor.
  2. Install the required NuGet package for Application Insights. You can use the Package Manager Console or the NuGet Package Manager UI to install the “Microsoft.ApplicationInsights.AspNetCore” package.
  3. Open the “appsettings.json” file in your project and add the following configuration:
"ApplicationInsights": {
  "InstrumentationKey": "YOUR_INSTRUMENTATION_KEY"
}

or use connection string

 "ApplicationInsights": {
    "ConnectionString": "take connection string from azure portal, go to app insghts overview blade, take it from right side in the top"
  }

Replace “YOUR_INSTRUMENTATION_KEY” with the instrumentation key you obtained from the Azure Portal.

  1. In the ConfigureServices method of your Startup class, add the following code to enable Application Insights:
services.AddApplicationInsightsTelemetry();

Optionally, you can also add additional configurations and customize Application Insights behavior. For example, you can set the sampling rate, enable or disable certain telemetry modules, or configure custom metrics and events.

app.UseApplicationInsightsRequestTelemetry(new RequestTelemetryOptions
{
    EnableW3CDistributedTracing = true,
    EnableSampling = true,
    MaxTelemetryItemsPerSecond = 5
});

app.UseApplicationInsightsExceptionTelemetry();

These additional configurations allow you to fine-tune how Application Insights collects and processes telemetry data based on your application’s specific requirements.

  1. Build and run your .NET Core Web API application. Now, your application is configured to send telemetry data to Azure Application Insights.

Verifying Application Insights integration:

To verify if the Application Insights integration is working correctly, follow these steps:

  1. Perform some actions or requests in your Web API application.
  2. Open the Azure Portal and navigate to your Application Insights resource.
  3. Explore the various monitoring and diagnostic features provided by Application Insights, such as performance metrics, dependency tracking, request tracking, and exceptions.
  4. Ensure that the telemetry data is being captured and displayed in the Azure Portal.

Exploring Application Insights features: Once you have successfully configured and verified Application Insights integration, you can explore its powerful features:

  1. Performance monitoring: Gain insights into the performance of your Web API application, identify bottlenecks, and optimize resource usage.
  2. Availability and failure analysis: Monitor the availability and health of your application, detect failures, and analyze error trends.
  3. Request tracking and dependency monitoring: Track incoming requests, outgoing dependencies, and their performance to understand the flow of your application.
  4. Custom telemetry and metrics: Instrument your code to track custom events, metrics, and exceptions, providing deeper insights into your application’s behavior.

Best practices for using Application Insights in your Web API application: To make the most out of Azure Application Insights, consider following these best practices:

  1. Enable and configure the necessary Application Insights modules based on your application’s requirements.
  2. Set appropriate sampling rates to balance telemetry volume and cost.
  3. Use custom events, metrics, and exception tracking to capture application-specific data.
  4. Regularly review and analyze the collected telemetry data to identify performance issues and areas of improvement.
  5. Monitor key performance indicators (KPIs) and set up alerts to proactively detect and respond to application failures or anomalies.

Conclusion:

In this step-by-step guide, you have learned how to set up Azure Application Insights in the Azure Portal and configure it in your .NET Core Web API application. By integrating Application Insights, you can effectively monitor and gain insights into your application’s performance, availability, and usage. With the telemetry data collected by Application Insights, you can identify and resolve issues quickly, optimize your application’s performance, and deliver a better experience to your users. Start leveraging the power of Application Insights to enhance your Web API applications today.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights