Creating an NLog wrapper that logs to Azure Table Storage

Welcome back to part two of our discussion on logging and tracing for .NET applications. In part one, we discussed a feature introduced in C# 5 that allows us to log detailed file, member, and line number information when tracing. Today, we’ll show how to build a wrapper for NLog that can utilize this functionality. We’ll also demonstrate how we can redirect our NLog output to Azure table storage. This will set us up for the final part of our blog, which will be building a log viewer for viewing logs stored in Azure table storage.

NLog is a logging platform for .NET with rich log routing and management capabilities. At Intellitect, we have used NLog for several large projects, and it has served us well. We have benefited from the configurable log routing to store our logs in the Windows Event Log, SQL Server, and simple text files. Today we will demonstrate adding another log storage destination, Windows Azure Table Storage. Before we can get to routing the logs, however, let’s build a logger in C# that wraps some of the common methods that are used when logging with NLog. Instead of wrapping NLog, we could of course fork the open source NLog project from GitHub and make our changes there, but to keep it simple, we will create a wrapper.

As you will see, our logging class is going to be very simple. Note that for simplicity, I’ve only included implementations for two of the numerous logging methods that NLog has. Specifically, I have code for the Trace method, which takes a message and up to four optional parameters that will be used to format the string using String.Format(…). This method logs using NLog level of Trace. I’ve also included code for the Error method, which takes an exception, a message, and up to four optional parameters that will be used to format the string using String.Format(…). This method logs using NLog level of Error. It is a trivial task to generate wrappers for any of the other NLog methods.

Note that in both of these cases, we can’t use a variable length params argument, because of the requirement that the CallerMemberName, CallerFilePath, and CallerLineNumber must be optional.

As you can see from the above code, our wrapper is very simple. Our class is static, and has reference to a single static instance of the NLog logger. For each wrapped NLog method, we simply take the input and format it using string.Format(…), then log it using the NLog logger. The only thing we’re changing, is always outputting the caller, file, and line number where the logging method was called from, by using the Caller info attributes that we discussed in part one of our blog. Additionally, we are tracing the message using System.Diagnostics.Trace, such that the data can be seen when running from within Visual Studio.

Now that we’ve essentially built the logger, let’s take a look at how we can route the output of the logger to Azure table storage. Due to the simple configuration model that NLog uses, redirecting our log output to Azure table storage is very simple. I’ll describe the steps to configure the output, assuming of course that you already have an Azure account with a storage account setup.

Step 1: Add the connection string for Azure storage to app.config

In the project you are using the logger from, open app.confg (web.config if it’s a web project), and add the following, putting in the values for your storage account that you get from the Azure dashboard.

Step 2: Add NLog Azure Table Storage target to your project

From Visual Studio, select Tools->NuGet package manager from the menu. From the package manager console that appears, type Install-Package AzureTableStorageNLogTarget. This will add NLog to your project.

Step 3: Create NLog.config, and set it to log to Azure

In the project you are using the logger from, create a file called NLog.config, with the following contents. Note that you may replace “TestLogs” and “SomeKey” with whatever values you’d like to use.

That’s it for configuration. Run the project, and you will be logging to Azure table storage! Next month, we will show how to build a UI for viewing these logs, but in the meantime, check out Azure Storage Explorer to view your table storage. Click here for the complete source code for this sample.

Join the Conversation

1 Comment

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

  1. I using latest version Nlog latest version 4.4.11 & i added extension of azure table storage(Nuget version 1.1.4) still TARGET TYPE Azure table storage giving error to me

    ‘ This is invalid xsi:type ‘

    Nlog.Config :