Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks 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:

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:

Creating an Application insight resource in Azure portal

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

Configuring NLog to define target to send telemetry to application insights

<?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.

Recent Blogs

Categories

NSS Note

Some of our clients

team