Wednesday, August 15, 2012

Implementing Logging with NLog

Logging is vital to any production application. The most obvious reason is when your application experiences some kind of issue, or worst, crashes on a production environment. Since you won’t have your debugging and development tools on a production machine (or at least, you shouldn’t), it is necessary to have logs that can tell you what the application was doing at the time it crashed.

Logging is as important as an efficient and structured way to handle exceptions. You need to make sure you are handling exceptions correctly and that you are logging, not only the exceptions when issues happen, but also a history of steps as your application moves through the user journey.

I have used a few different tools and frameworks for logging implementations like Microsoft’s Enterprise Library and log4net, although they might be overkill for a simple logging solution. You can also write your own logging classes, after all, the point is to generate a text file with app workflow information, debug information and issues/errors details, but why invent the wheel all over again, right?

That being said, I wanted to share a sample prototype on how to implementing logging functionality in your application with NLog, in a simple, quick, step by step fashion. So here we go.

First: you need to download and install the NLog package. The easiest way is to do it through NuGet. If you haven’t used NuGet, check out this blog post about the subject.

image

Second: Now that you have the package reference, you need to add an NLog configuration file. Just right click on your project, select Add New Item, and select the Empty NLog Configuration File.

image

Third: Since NLog requires the configuration file to be copied to the application folder, you need to make sure that the NLog.config file you just added is copied to the output app folder. Right click on the NLog.config file and select Properties. Then make sure that the Copy to Output Directory is set to Copy always.

image

Fourth: now that all the configuration and package settings are set, we can create a logger. By design, NLog creates a logger per class. This just means that every class in your project will have its own logger, but all of them will write to the file you want, which is generally a unique text file that you will specify later. The advantage is that NLog will identify the class automatically, making it easier for you to identity application workflow steps and clearer log information. To create a logger you will use NLog LogManager.

You can specify the name of the logger as follows:

Alternatively, you can let NLog to name the logger automatically based on the class name:

Fifth: NLog has six different log levels:

  • Trace - very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during development.
  • Debug - debugging information, less detailed than trace, typically not enabled in production environment.
  • Info - information messages, which are normally enabled in production environment.
  • Warn - warning messages, typically for non-critical issues, which can be recovered or which are temporary failures.
  • Error - error messages.
  • Fatal - very serious errors.

So, now you can simply create a method and log some messages. Here’s an example on how to do it:

Sixth: Now that we have learned how to emit log messages, we need to configure the logging output on the NLog configuration file. you need to define the target file and the rules to have a basic configuration file setup. Here’s what your NLog.config file should look like:

And you are done. Now you have your app logging messages at different log levels, for any class you want on your application. This are screenshots from the sample app:

image

image

image

Make sure to check out NLog’s documentation for more advanced topics and different ways to use the loggers and do advanced configuration. Also if you don;t fancy using NuGet for some reason, you can download NLog bits directly from their site here.

Hopefully this will help you to get started quickly and easily. It certainly helped me out and is a perfect fit for a quick application logging implementation. You can download the sample application code from my Github repo here.

No comments:

Post a Comment