Application insight provides the logging mechanism for our application.
It is also helpful to troubleshoot any exceptions occurring in our application.
It is also helpful for data analytics-type scenarios.
It is also helpful in alerting and monitoring; in case any exception occurs to our application, it automatically executes the alert as well.
Application insight is an important aspect of instrumentation key.
The instrumentation key we need to configure on our application side will store the log inside the application insight.
Creating an application insight resource in the Azure portal
- Create the account on Azure portal (https://portal.azure.com) and login.
- Click on the create a Resource and search the application insights.
- Click on the Create button.
- The screen will be open for the creation of the application insights resource.
- The subscription is required. if you don’t create the subscription. You need to create the subscription and use it here.
- A resource group is required. If you have already created the resource group, then you can use that or create the resource group.
- The name is required. You need to mention the application insights name and based on your provided name the application insights resource has been created.
- The region is required. You need to mention which region your resources will be created.
- Resource Mode is required. Select the Workspace-based resource mode.
- Log analytics workspace is required. If you have already created any log analytics that you want to use here, or you need to create a new one and associate it here.
- Once you have associated the required details click on the Next:Tags button.
If you want to add any kind of tags, you need to add them here. This step is optional.
- Click on the Next : Review + create > button.
- Click on the Create button. It will create the resource under the resource group as you have mentioned above.
- After you see the below screen.
- Once it creates the resource, then you can see the below screen.
- Click on the Go to resource button. It will redirect to your application insights resource.
- You need to instrumentation key for the integration of the application insights in your .net core application.
Integrate the Application Insight to the .Net Application
In the article below, we already see how to implement NLog.
https://www.c-sharpcorner.com/article/nlog-implementation-in-net-core-application/
We need to install the application insight-related NuGet package into our application. Please follow the below steps:
- Open the project whenever you want to implement application insight.
- Right-click on the solution and click on Manage NuGet Packages.
- Need to couple of the packages installed in your application.
- Search in the search box for Microsoft.ApplicationInsights.AspNetCore.
- Search in the search box for Microsoft.ApplicationInsights.NLogTarget.
- Select the version based on your application and click on the install button. Please refer to the below screenshot for more details.
- Once it is installed in your system, you can verify it on the below screenshot.
Now that you need to update the nlog.config file, please follow the below steps:
<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="Info" writeTo="aiTarget" />
</rules>
After that, on your code side, we need to add the dependency for application insights. Please follow the below steps:
Go to your Program.cs file and add the below code.
builder.Services.AddApplicationInsightsTelemetry();
The above line enables Application Insights telemetry collection.
Once you have completed the above steps, run your .net application and execute your API so he can store the logs in your application insight resource.
Please follow the below steps for the verification.
- Go to your application insight resource. Click on the logs button on the overview page or search the logs in the search box.
- Click on the Logs. You can see the below screen.
- Write the query here like traces and mention the time range if you want to, and click on the run button so he can fetch the logs and show you in the result window.
- If you want to see the details levels log, then click on the arrow so he can provide every detail.
- If you want to see the exception regarding the logs, then in the query window you need to search for exceptions, then it will show you the exceptions log.
- If you want to filter the message, then you can also do it.
- traces | where message contains "NLog"
- Click the run button.