Application Insights with NLog

One of the things I love with programming is debugging. On my recent projects, I have implemented logging via NLog with Application Insights which greatly improves my debugging time.

To start, I’m assuming you’re developing a .net web application with Visual Studio 2017 and an Azure subscription.

  1. Open your application.
    Solution Explorer
  2. Right click on the solution and select Manage NuGet Packages for Solution….
    Manage NuGet Packages
  3. Click on the Browse Tab and type NLog
    NuGet Browse
  4. Select your projects and click Install.
    NuGet NLog Install
  5. Right click on your project and select Application Insights and then Configure application insights.
    Configure Application Insights
  6. Select your Azure subscription and resource group.
    Register Application Insights
  7. To prevent any charges for application insights select the option of “Application Insights will remain free and halt data collection after 1GB/Month.” And then click Register.
    Please don’t choose this option for a production application.

At this point your application is now configured to use application insights and send the data to Azure.
Next we’ll add logging via NLog.

  1. Open the HomeController.cs file
  2. Add using NLog; to the top of the file.

    private static ILogger _log => LogManager.GetCurrentClassLogger();

    to the first line after

    public class HomeController : Controller
  3. And
    _log.Debug("Entered the Home Controller Index Action.");

    inside the

    public ActionResult Index()
  4. Finally deploy your application and with in a few minutes you will see debug data available in Application Insights on Azure.

Full source code available: GitHub

Leave a Comment

Your email address will not be published. Required fields are marked *