:::: MENU ::::

Adding Application Insights in ASP.Net core project

Application Insights is an extensible analytics platform that monitors the performance and usage of your live Asp.Net core applications.

In this article, I will show how to integrate application insight in ASP.Net core project.

We will need:

  • Azure Subscription
  • Visual Studio 2015 update 3 with latest .Net core tooling

– First create an Application Insight resource in azure.

In Azure portal, Click New -> Developer services -> Application Insights. In that form give resource name, select application type as ASP.NET web application, select subscription, resource group and hit create.

-Now Create a project in visual studio.

GoTo FNP (File -> New -> Project) and select Web node under visual c# and create ASP.Net core web application that build on .net core.

NOTE: There is a checkbox in right hand side of new project dialog which gives option to add application insights SDK in our project during creation time. Also you can create app insights resource directly from there but lets skip that part for now and just add SDK only and hit OK then select web application without authentication and hit OK.

This approach will add all necessary bits which are needed to configure App Insights in our code. Let’s dig a little bit into that. If you create project without checking Application Insights checkbox in project dialog box then you need to add below mention code manually.

  • Go To project.json file.

Above nuget package of application insight is added in our project which contains all APIs of app insights. You can verify the version number for the latest package from app insights release page.

NOTE: In case of .NET Core applications, if you run into restore errors with respect to application insights dependency, please add “dotnet5.6” and “portable-net45+win8” to the imports list (if it does not exist), under frameworks section of project.json, as shown below.

  • Lets check appsettings.json file.

Add instrumentation key of your Application Insights web application resource which you can get from azure in ApplicationInsights section of appsettings file.

ApplicationInsights section is already added during project creation time.(Remember that checkbox)

There is another option available for setting Telemetry Channel which you can set if you want.

Now lets see what bits are added in startup.cs on behalf of application insights.

  • In startup.cs constructor

appsettings.json has been added to our configuration builder so our settings related to Application Insights has also added. Now we can access that settings from anywhere inside our application.

Application Insights setting is set to developer mode if application is in development environment. Which helps to push telemetry data through app insights pipeline faster.

  • In ConfigureServices method

We are registering Application Insights services inside this method so that we can use app insights service later from anywhere inside our application.

Here we can see AddApplicationInsightsTelemetry() method take configuration as a parameter for service initialization. Configuration variable contains all the settings related to Application Insights which helps app insights to get initialize.

If you are confuse on how passing configuration property helps to get app insights service to initialize then here is how it works behind the scene. Following code is from Application Insights github repo.

You can see how configuration variables are setup for getting application insights data. i.e. Instrumentation key, Developer mode and Endpoint address. So  during runtime InstrumentationKeyFromConfig variable is used to get Instrumentation key from config file. Because it contains exact JSON link of where we stored key i.e. Inside ApplicatioinInsights section in InstrumentationKey key (“ApplicationInsights:InstrumentationKey”).

  • In Configure method

Here two tracking middleware are added.

NOTE: Request Telemetry should be added as the very first middleware in pipeline then only Exception Telemetry. Also Exception middleware should be added after error page and any other error handling middleware:

If you don’t have any error handling middleware defined, just add this method right after UseApplicationInsightsRequestTelemetry method.

So now we have configured Application Insights. Now let’s see how to collect data inside our code and send it to azure.

-Add Tracking Logic:

There are multiple options available to track different scenarios. Here i will show you only two option. You can explore other options too.

In Index action of Home controller, I have tracked page view and exception with setting basic options. You can include other properties too. Like Duration, Metrics, Properties etc. of that particular scenarios.

Now run your application and navigate to index (which will navigate by default) and do a refresh few times then you will start to see data in azure. Also locally in visual studio.

In Azure:

This is how you can add application insights in ASP.Net core web application. Sample code is available in Github. Explore more about Application Insights from here.

Happy coding! 🙂