Using NLog To Track Events

Instrumenting your application, or at a more basic level, simply logging specific events, is one of the most important things you can do for a system that going to have a productive and cost effective lifespan. It’s right up there with data backups.

As with most things in .Net, its usually best accomplished by starting with something you didn’t write from scratch; the starting point could be something in the .Net base class library or in this case, an open source library.

When .Net was the new thing, the team that would eventually become the Patterns and Practices team at Microsoft came out with a few frameworks. One of these was the Exception Management Application Block, or EMAB. It was a tight little, yet extensible, library that would log events in your application to a file on disk, a database, or even send them as an e-mail to your blooberry in case you were at dinner with your spouse. As with most things that have an active set of developers with a mandate, it grew considerably into the Exception Handling Application Block and the Logging Application Block, currently bundled into the 4.0 version of the Enterprise Library.

The Enterprise Library is a nice place to dive in when you need common horizontal structures like data access, logging, cryptography, and security. There’s quite a bit of technology there. Mastering how to use the entire library would put you in a good tier of application developers.

You don’t have to stay in the same neighborhood though, there is a good amount of fun in trying some other popular libraries. See, I’m not that concerned by that last sentance, because if you made it this far, you’re already in the club. Those that mock folks who claim that poking around in event logging libraries is fun have already been turned away by the first two paragraphs, so you’re among friends here.

I was watching the 20th installment of the MVC Storefront last week. Rob Conery is building a store with the ASP.Net MVC bits. This episode introduces Logging to the solution and Rob chose a logging library named NLog. Its a very inobtrusive logging framework that bolts on quick. He said he chose this over Log4Net, as it involved too much XML configuration. I guess Rob would have disliked the Enterprise Library too. There’s so much XML configuration that the Enterprise Library includes a little Windows client app to help you configure it visually.

So, in NLog, you reference the library in your solution, drop in a special file named NLog.config in your web application root folder, and then run this code when you want to log something:

var logger = LogManager.GetCurrentClassLogger();
logger.Error("error message goes here");

The appeal of NLog is in the NLog.config file. Here’s an example of a simplistic configuration that logs Debug events (or worse events) to a file on disk.

Simple NLog Configuration

Here’s an example of a configuration that automatically archives the log file when it grows beyond a certain size.

Auto Archiving NLog Configuration

NLog can work directly inside your own code base, but its better if you inject a little abstraction so you’re not married to it. You’re heading down a good path when you create an interface, as Rob shows in his video, and then implement that interface with code that calls the NLog library. Using this pattern, you can still swap out NLog for another framework without a lot of fuss and bother.

When you’re sprinkling these logging calls in your solution, you’ll select the semantics for a given message as Trace, Debug, Info, Warn, Error, and Fatal. If you’re adding Trace and Debug messages, you’ll probably record these in a database so you can analyze them later with some SQL queries or LINQ code. Disable these in your production code with a precompiler directive like #debug. The other semantic tags are important for your release solution. NLog can help you be agile about where each type of event gets logged. You can direct where the event is logged by its type (Info, Debug, Error, etc) or some other condition.

The simplicity and agility of this logging framework make it one of my favorites. Check it out on your own some evening or weekend.


No Comments on Using NLog To Track Events

Comments on this entry are closed.