C#: NLogging to the local File System and to LogEntries

>inb4 title spellcheck
Yes, the N is supposed to prefix “logging” in the title.
Because Nlog.
Nlog? you ask.
Yes. Nlog.

Nlog is an open source logging platform for .NET, Silverlight and Windows phones. Installation via NuGet, insanely easy to implement and packed with all the features you’d want in a logger, it’s an ideal logging solution.

In order to install Nlog, just open the NuGet Package Manager and search for Nlog. There’s two packages that are needed – NLog and NLog configuration. NLog is the actual library and NLog configuration is the NLog’s config file for determining the log levels and locations.


Once both of the packages are installed, the configuration file requires a tiny bit of tweaking. When I say tiny bit of tweaking, I mean, MICROSCOPIC tweaking. Two lines and we’re done. The examples given in the NLog.config are perfect for understanding how to get started.


Once the config file looks pretty and setup as needed, it’s time to invoke NLog in the application. This is as simple as creating a using statement for NLog and then creating a logger with a given name. You can create a log with a name by calling LogManager.GetLogger(“name”);. NLog’s documentation indicates that in most cases you’ll have one logger per class – which if you create a logger inside of a class you can just use LogManager.GetCurrentClassLogger().

In my testing I decided to replace the exception logging I had put in place in a previous post where I had created my own logging class. NLog provides multiple methods for different levels of logging. The config file lets you choose the minimum level in which gets written to the log.

  • Trace
  • Debug
  • Info
  • Warn
  • Error
  • Fatal

Since I’m replacing an exception logger and the exception is a wee one, I’ll use the Error() method.  (Don’t make fun of my code >.< it’s my first home-baked app)


Then when I run the application and null out one of the fields


Now, this is pretty cool and what not, but writing logs to LogEntries would be even cooler, right? Well guess what, it’s freakin EASY to implement as well. LogEntries provides a copy/paste template that goes into your applications app.config for web.config – the only value you have to change is the token to your logger.

Visit https://logentries.com/doc/dotnet/ to copy the entries.

You can create a free account on LogEntries and try out their service for 30 days. When you create an account you’re prompted to create a new host which will contain a set of logs. Once the host is created you can create your first log. Once the log is created, you’re given a token which will be placed in your app.config. This all happens right after each other when you create the account – few seconds and done.

Here’s the app.config once it’s all said and done


I Could just run the app now and it would write the logs to LogEntries, but I want to implement the log on an actual class and use the GetCurrentClassLogger() method.

private static Logger logger = LogManager.GetCurrentClassLogger();

then called the logger’s info method to tell me when the my Person’s data changed


And when running my application and hitting submit



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s