Looking for an Expert Development Team? Take two weeks Trial! Try Now
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.
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.
Step 1: Create an account on Azure portal (https://portal.azure.com) and login.
Step 2: Create a resource group under which we will create application insight resource.
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.
Step 3: Once the resource group is created, we have to create an application insight resource under the resource group.
As shown in screenshot below, click on add button and search for “DevOps”. Under this category, you will find “Application Insights”. Click on it.
Step 5: Click on “Create” button and it will display the options to enter the name of AppInsights resource. A green tick in front of it shows that the name of such resource is valid.
Step 6: Finally, create the resource and Azure will start deploying the resource under the resource group.
For all the integrations to any type of application, we just need an “Instrumentation Key” that uniquely belongs to an Application insight resource.
Now, we are done with all the setup in Azure portal. Let’s to application integration.
Creating an ASP.NET Core 3 App and configuring application insights
Step 1: Start Visual Studio 2019 and Create a new project.
Step 2: Select ASP.NET Core Web Application from Project templates.
Step 3: Name the project as per your need and click on Create button.
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.
Step 5: Once the project is created successfully, right click on project name and go to Add -> New Item -> Application Insights Telemetry.
Step 6: Click on Connected Services option and you will find application insights as one of the option in the window.
Step 7: Once you click on Get Started button, it will start adding nuget packages that corresponds to application insight to the project.
Step 8: Login to Microsoft account using the same credentials with which you created your Azure account. Once you login, it will automatically show you the application insights resource that we created in the azure portal. Select the same resource and click on Register.
Once you follow the above steps, we are now good with integration of application insights in asp.net core app. Now, let’s look at the major places where dependencies are registered in the app.
Since the start points of the application is Startup.cs file, so we will see that Azure application insight service is injected in Configure Services method.
The instrumentation key is referenced in appsettings.json file where all the configuration of the application is placed. This is the most important thing in the whole setup. If you want to use any other application insight resource in future, you just have to replace this instrumentation key with new one and nothing else is required.
Configuring NLog to define target to send telemetry to application insights
Step 1: Right click on project name in solution explorer and click Manage nuget packages.
Step 2: We need to install 3 packages to configure NLog.
1. NLog.Web.AspNetCore – this package will install the base dll’s for NLog support for .net core projects.
2. Microsoft.ApplicationInsights.NLogTarget – this package will install a dll that allows NLog to send telemetry to Azure portal.
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.
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.
Secondly, configure NLog in Program.cs file to capture logs at the application start.
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.
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.
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.