Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Integrating Application Insights with NLog in ASP.NET Core 3 Web App

Application Insight, a monitoring service offered by Microsoft Azure. With the help of Application Insight, we can monitor the performance of the application. It can be used to assess the live web app as well. It has very useful data analytics tools which empower people to easily diagnose problems related to applications and servers.

Features of application insights:

  • Request and response capture
  • Dependency capture
  • Exception logging
  • Page views
  • Loading performance and diagnostics
  • User sessions
  • Custom events and metrics
  • Many more…

To use Azure, we need to subscribe to it, and we would also need Azure credits. You can register on https://portal.azure.com and get a 30 day free trial or you can activate a Microsoft developer license which gives you access to this portal and azure services.

The application insights provide a lot of integration with almost all technologies out there in the market whether it's ASP.NET, Angular, NodeJS, etc. Nowadays, it is the most extensively used service by almost all big players in the market.

Agenda: In this example, the Asp.net application Development company will be developing an ASP.NET core of 3 apps. We also integrate the application insights services to get the performance insights, including errors and logs. We also are creating application insights resources in the Azure portal. An additional library, NLog, that offers the flexibility to capture logs in an improved manner will be used as well.

Pre-requisite:

  • Visual Studio 2019
  • Subscription to Azure portal with Azure credits

Creating an Application insight resource in Azure portal

Step 1: Create an account on Azure portal (https://portal.azure.com) and login.

Step 2: Create a resource group and an application insight resource within it.

image001
image002

Step 3: Select your subscription and enter a valid resource group name. It can be anything and a green tick in front of it shows its availability.

image003
image004

Step 4: After creating the resource group, make an application insight resource under the resource group.

image005
image006

As shown below, click the add button. Then, search “DevOps”. Under this category, you will find “Application Insights”. Click on it.

image007

Step 5: Click the “Create” button to get the options to add AppInsights resource’s name. If the name of such resource is valid, you will get a green tick.

image008

Step 6: Lastly, create the resource. Then, Azure will deploy the resource under the resource group.

image009
image010
image011

For all the integrations to any type of application, we just need an “Instrumentation Key” that uniquely belongs to an Application insight resource.

image012

After the setup is done, we will do application integration.

Creating an ASP.NET Core 3 App and configuring application insights

Step 1: Start Visual Studio 2019 and Create a new project.

image013

Step 2: Select ASP.NET Core Web Application from Project templates.

image014

Step 3: Name the project as per your need and click on Create button.

image015

Step 4: Select .NET Core and Version as 3.0 from the selection boxes and select Web Application from the templates.

We don’t need authentication, so untick that option along with Https option. Finally to create project, click on create button.

image016

Step 5: Once the project is created successfully, right click on project name and go to Add -> New Item -> Application Insights Telemetry.

image017

Step 6: Click on Connected Services. Look for Application insights as one of the choices in the window.

image018

Step 7: Click Get Started button to add the Nuget packages that parallels with the application insight to the project.

Step 8: Login to your Microsoft account with the same credentials that you used to create your Azure account. After logging in, the application insights resource will be depicted automatically. Select the resource and Click Register.

image019
image020
image021

After following all the steps the integration of application insights in the ASP.NET core app is completed. We will now explore the main areas where dependencies are registered in the application.

Startup.cs file is the start point of the app. Azure application insight service is implemented within the Configure Services method.

image022
image023

The instrumentation key is referenced in the appsettings.json file. For using any application insight resource later on as well, the users will be required to replace the instrumentation key with a new one. That’s all.

image024

Configuring NLog to define target to send telemetry to application insights

Step 1: Right click on the name of the project in the solution explorer. Afterwards, click the Manage nuget packages.

image025

Step 2: 3 packages are required to be installed to configure NLog.

1. NLog.Web.AspNetCore – It will install the base dll’s for NLog suppor. This can be done for the.net core projects.

image025

2. Microsoft.ApplicationInsights.NLogTarget – this package will install a dll that allows NLog to send telemetry to Azure portal.

image026

3. NLog.config – this package will add a configuration file to the project where we can define targets. This file will be read by NLog to capture logs.

image027

Step 3: After installing all the required packages, we have to configure NLog in the project pipeline so that .net core knows about NLog.

Replace the code in NLog.config file with the below.

image028
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <extensions> <add assembly="Microsoft.ApplicationInsights.NLogTarget" /> </extensions> <targets> <target xsi:type="ApplicationInsightsTarget" name="aiTarget"> <instrumentationKey>Your application Insights Instrumentation Key</instrumentationKey> <contextproperty name="threadid" layout="${threadid}" /> </target> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="aiTarget" /> </rules> </nlog>

Secondly, configure NLog in Program.cs file to capture logs at the application start.

image029

Now, we are done with all configurations and setup. Let’s run the application in debug mode and check if logs are going into Azure portal.

image030

Navigate through the links in the app. With this, relatively more logs can be generated. After then, log in to the Azure portal to review all the logs.

image031

NLog allows several targets at the same time, therefore, it is preferred extensively. In the example here, we only configured one target. However, only by modifying the NLog.config file with new targets and the related rules, we can send telemetry to other targets, including the files, database, and other services.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients