NLog Implementation with Azure Application Insights

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

  1. Create the account on Azure portal (https://portal.azure.com) and login.
  2. Click on the create a Resource and search the application insights. 
    Creating an application insight resource in the Azure portal
  3. Click on the Create button.
  4. The screen will be open for the creation of the application insights resource. 
    Creating an application insight resource in the Azure portal
    1. The subscription is required. if you don’t create the subscription. You need to create the subscription and use it here.
    2. A resource group is required. If you have already created the resource group, then you can use that or create the resource group.
    3. 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.
    4. The region is required. You need to mention which region your resources will be created.
    5. Resource Mode is required. Select the Workspace-based resource mode.
    6. 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.
  5. Once you have associated the required details click on the Next:Tags button.
    Creating an application insight resource in the Azure portal
    If you want to add any kind of tags, you need to add them here. This step is optional.
  6. Click on the Next : Review + create > button.
    Creating an application insight resource in the Azure portal
  7. Click on the Create button. It will create the resource under the resource group as you have mentioned above.
  8. After you see the below screen. 
    Creating an application insight resource in the Azure portal
  9. Once it creates the resource, then you can see the below screen.
    Creating an application insight resource in the Azure portal
  10. Click on the Go to resource button. It will redirect to your application insights resource.
    Creating an application insight resource in the Azure portal
  11. 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:

  1. Open the project whenever you want to implement application insight.
  2. Right-click on the solution and click on Manage NuGet Packages.
  3. Need to couple of the packages installed in your application.
    1. Search in the search box for Microsoft.ApplicationInsights.AspNetCore.
    2. Search in the search box for Microsoft.ApplicationInsights.NLogTarget.
  4. Select the version based on your application and click on the install button. Please refer to the below screenshot for more details.
    Integrate the Application Insight to the .Net Application
    Integrate the Application Insight to the .Net Application
  5. Once it is installed in your system, you can verify it on the below screenshot.
    Integrate the Application Insight to the .Net Application

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.

  1. Go to your application insight resource. Click on the logs button on the overview page or search the logs in the search box.
    Integrate the Application Insight to the .Net Application
  2. Click on the Logs. You can see the below screen.
    Integrate the Application Insight to the .Net Application
  3. 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.
    Integrate the Application Insight to the .Net Application
  4. If you want to see the details levels log, then click on the arrow so he can provide every detail.
    Integrate the Application Insight to the .Net Application
  5. 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.
  6. If you want to filter the message, then you can also do it.
    1.  traces | where message contains "NLog"
    2. Click the run button.


Similar Articles