Adding Application Insights to a .NET Core Site in Azure

   —   

Once you select Azure as your hosting platform, a vast world of resources becomes available. From CDNs to Load Balancers, Microsoft’s cloud has loads of great services to help you develop and maintain your applications. Because performance is king, adding Application Insights to your site is a great way to stay on top of issues and understand how your application is performing. In this article, I’ll show you how to add this great monitoring tool to your .NET Core applications.

Note that I will be using Visual Studio 2017 for this article, so some of the screens may look different if you are using a different version.

Adding the service

The first step in the process is to add Application Insights as a Connected Service. In Visual Studio, you can right click your ConnectedServices folder and select Add Connected Service.

Add Connected Service

This action starts the configuration process for your Azure account. You will be prompted for your subscription credentials and resource group settings. Once you get everything set, you will see a message detailing the progress.

Installing

After the install is complete, you should see a completed configuration screen, showing everything is set correctly.

Configured

Confirming the code changes

By going through the installation process, you are updating your project with the necessary NuGet packages and dependencies. This means there will be changes to your project configuration. Here is the Nuget package that gets installed.

NuGet Package

Additionally, your appsettings.json file will be updated with your AI InstrumentationKey. This key lets Azure know which AI service to track your website activity to.

appsettings.json

Local testing

Once you have the packages installed and configured, fire up your site locally to test. You should see a new ApplicationInsights option in your toolbar.

Toolbar

In my case, I got an error as soon as I tried to launch my site. Oh snap!

Registration error

You may get this error if you don’t have the right code in your Startup.cs file and your layout. You can easily add the following code to resolve it:

Startup.cs

public Startup(IHostingEnvironment env) { …. if (env.IsDevelopment()) { builder.AddApplicationInsightsSettings(developerMode: true); } } public void ConfigureServices(IServiceCollection services) { … // Add Application Insights services.AddApplicationInsightsTelemetry(Configuration); }

 And then in your _Layout.cshtml file, you need to add your JavaScript.

@Html.Raw(JavaScriptSnippet.FullScript)

With those code changes, you should be able to launch your site and see your telemetry data.

Local testing

Viewing on your live site

After adding your code changes and configurations, you can deploy your changes. After deploying your updated site to Azure, click on the Application Insights option within your App Service.

Live stats

On this screen, you can view how your server is performing, average page loads times, which pages are taking the longest, and much more. You can drill down into any of the stats and see detailed information, as well.

Detailed view

Moving Forward

Adding Application Insights to your applications is a great way to get a detailed understanding of how your site is performing. Because everything is Azure hosted, you have access to all aspects of the project, including server stats, response times, and even dependencies. And the best part? It’s free for the first gigabyte of telemetry data stored per month, so you can add AI to your existing Azure sites and start gathering data. Good luck!

Share this article on   LinkedIn

Bryan Soltis

Hello. I am a Technical Evangelist here at Kentico and will be helping the technical community by providing guidance and best practices for all areas of the product. I might also do some karaoke. We'll see how the night goes...