6 steps to integrate Application Insights with .Net Core application hosted in Azure App Service

Prerequisites

  1. Visual Studio 2019 (or Visual Studio code)
  2. Create an Azure App Service

In this article we will learn the steps to integrate a .Net Core Web API with Application Insights which helps you to log all the application telemetry and gain insights about Failures and Performance with very low-code.
Let’s start.

Advertisements

Step1 –  Create a new ASP.Net Core Web API Project

Create a new ASP.Net Core Web API using Visual Studio as shown below.

Azure Application Insights - Create a new ASP.NET Core Web API Application
Azure Application Insights – Create a new ASP.NET Core Web API Application

Once the application is created, it looks like this

Azure Application Insights - Created Web API Application

Run the application to see if it’s running in the local machine by pressing F5. It should run in some port as shown below.

Azure Application Insights - Created Web API Application - Local API output
Advertisements

Step2 Download Microsoft.ApplicationInsights.AspNetCore Nuget package and configure Log Levels

Once the application is working n the local environment, let’s add the libraries that can be used to log the errors / telemetry to the Application Insights

Azure Application Insights - Install Microsoft.ApplicationInsights.AspNetCore Nuget Package

Install the Microsofot.ApplicationInsights.AspNetCore nuget package shown above. Once you install, you can see the package added to the Project file as shown below.

Azure Application Insights - Project File with Microsoft.ApplicationInsights.AspNetCore Nuget Package
Azure Application Insights – Project File with Microsoft.ApplicationInsights.AspNetCore Nuget Package

In the appSettings.json add the Application Insights Log Category as shown below.

Azure Application Insights - App Settings File with ApplicationInsights Log Level
Azure Application Insights – App Settings File with ApplicationInsights Log Level

Build the application to see if everything is configured properly.

Advertisements

Step3 Enable Application Insights and configure Dependency Injection of ILogger interface

In this section, we will enable Application Insights in the ConfigureServices method of the Startup class as shown below.

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddApplicationInsightsTelemetry();
}

In order to enable the Controllers to leverage Log Providers (in our case, Application Insights), we need to add ILogger interface to the Constructor as shown below.
private readonly ILogger _logger;

public WeatherForecastController(ILogger logger)
{
_logger = logger;
}
Advertisements

Step4 – Log the Errors, Information and Traces in your application code

Now, we can start logging the errors, information and any traces in your application code using _logger object reference as shown below.

6. Azure Application Insights - Log Errors
Azure Application Insights - Publish to Azure App Service
Advertisements

Step5 Deployment of the Web API application to Azure App Service

As one of the Prerequisites was to create an Azure App Service – Web App, this section assumes that you have created an Azure App Service.
Let’s deploy the application to Azure App Service by right clicking on the Project and click on Publish and choose the appropriate options in the Publish window. If you have configured everything properly, you would see something as shown below.

Step6 – Create Application Insights and integrate with Azure App Service
The final step is to create an Application Insights instance and integrate it with the Azure App Service by following the below steps.

Create an Application Insights and copy the Instrumentation Key as shown below.

Azure Application Insights - Copy Instrumentation Key
Azure Application Insights – Copy Instrumentation Key
9. Azure Application Insights - Add App Settings - Instrumentation Key

Navigate to the Configuration blade of the Azure App Service and add a new App Setting as shown below and click on Save to save the changes.

Tip: Ensure you create the Azure App Service and the Application Insights in the same Location. If not, if would lead to performance issues.

Finally, access the action method and after 2 minutes (sometimes up to 5 minutes), you should be able to look at the logs as shown below.

Azure Application Insights - View Traces


That’s it. We have learnt how to integrate Azure Application Insights with a .Net Core 3.1 application which is hosted in Azure App Service.
In the upcoming modules, we will learn more about Application Insights features.
Happy Learning.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s