How to connect dotnet core application and App insights in Azure

How to connect dotnet core application and App insights in Azure

·

2 min read

Application Insights is a feature of Azure Monitor that provides extensible application performance management (APM) and monitoring for live web apps. Developers and DevOps professionals can use Application Insights to:

  • Automatically detect performance anomalies.
  • Help diagnose issues by using powerful analytics tools.
  • See what users actually do with apps.
  • Help continuously improve app performance and usability

To use Application Insights, you either install a small instrumentation package (SDK) in your app, or enable Application Insights by using the Application Insights agent.

The instrumentation monitors your app and directs the telemetry data to an Application Insights resource by using a unique instrumentation key

image.png

How to Connect Application insights with a dotnet core application ?

  • Create an Application Insight resource in the Azure portal.
  • Provide required fields image.png
  • Create a .Net Core Application in Visual Studio.
  • In the tools section find the Nuget Package Manager and navigate to Package Manager Console
  • It will open the console at the bottom of visual studio
  • Paste the command: dotnet add package Microsoft.ApplicationInsights.AspNetCore --version 2.21.0-beta1
  • In the program.cs Please add the Services.AddApplicationInsightsTelemetry(); in the ConfigureServices Method. This line enables the Application Insights telemetry collection.
  • In the Appsettings.json file create a new section called ApplicationInsights
    "ApplicationInsights": {
    "InstrumentationKey": "<<Get instrumentation key>>"
    }
    
  • We can get the Instrumentation Key from the App Insights resource and paste it into the above json App Insights.png

what is Instrumentation key ?

  • The instrumentation key identifies the resource that you want to associate your telemetry data with. You will need to copy the instrumentation key and add it to your application's code
  • The instrumentation monitors your app and directs the telemetry data to an Application Insights resource by using a unique instrumentation key. The impact on your app's performance is small. Tracking calls are non-blocking, and are batched and sent in a separate thread.
  • You can instrument the web app, any background components, and the JavaScript in the web pages themselves. The app and its components don't have to be hosted in Azure

Now build and Run the application in you local. Navigate to the Azure App Insights resource that has been connected through the Instrumentation key.

In the resource search for the Live Metrics. After few minutes you can see the graph in the live metrics changes based on the request

image.png

We can monitor, Detect and diagnose whenever you receive an alert or discover a problem like:

  • Assess how many users are affected.
  • Correlate failures with exceptions, dependency calls, and traces.
  • Examine profiler, snapshots, stack dumps, and trace logs