JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Software Development Team? Take two weeks Trial! Try Now

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

What is Application Insight: It is a monitoring service provided by Microsoft Azure. It helps to monitor application performance, errors, logs, etc. You can use it to monitor your live web application. It provides powerful data analytics tools that helps in diagnosis of application issues as well as server issues on which those apps are deployed.

Features of application insights:

You need a subscription to Azure portal along with some azure credits. You can register on https://portal.azure.com and get a 30 days 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 market whether its ASP.NET, Angular, NodeJS, etc. Now-a-days, it is the most extensively used services by almost all big players in the market.

Agenda: In this example, Asp.net application Development company will create an asp.net core 3 application and integrate application insights services to capture logs and events that happen when we run the application. We will also learn to create application insights resource in azure portal and use the same in the application. We will be using an additional library which provides us the flexibility to capture logs in better ways. The library is NLog.

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

Try to navigate through the links in the application so that more logs can be generated and then login to Azure portal to check the logs.

image031

The beauty of NLog is that it allows multiple targets at same time. That means that in this example, we only configured one target, but if we want to send telemetry to other targets like database, file, or any other service, we just need to modify NLog.config file with new targets and corresponding rules and we don’t have to make any code changes.

 

Some of our clients

team