Loggin’ Like Bunyan: An NLog Tutorial

Enterprise Library has been my go-to for logging for the past few years. This is largely because I’ve worked on a project that was using it, and it did what I needed. It was relatively easy to use, and I didn’t really have a reason to look elsewhere.

However, I was working on a new project that needed some logging, and I decided to explore some other options on a whim. Another team in the company was using NLog, so that’s where I went first. I gotta say, it seems a lot lighter weight and easier to use than Enterprise Library. It’s all available through NuGet, so adding it to any new project is a breeze.

Adding support for NLog to your application requires just two things: a reference to NLog.dll and an NLog.config file. Here’s how you can go from zero to logging in three steps.

  1. Install the “NLog Configuration” package from NuGet. This gives your project a reference to NLog.dll, the NLog schema (for Intellisense when editing NLog.config), and an empty NLog.config.
  2. Edit NLog.config to have a target and a rule. Here’s a very basic log-to-file config:
    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        <target name="logfile" xsi:type="File" fileName="log.txt" />
        <logger name="*" minlevel="Info" writeTo="logfile" />
  3. Get to loggin’! Create a Logger and use it.
    var logger = LogManager.GetCurrentClassLogger();
    logger.Info("Loggin' like Bunyan!");

It’s really that simple. The above config and logging code produce the following log entries:

2013-10-29 10:18:53.7819|INFO|NLogSample.Program|Loggin' like Bunyan!

Create a Shortcut to Open a Website in a Non-Default Browser

In a perfect world, you’d be able to use your preferred browser to visit all your websites and browser-based applications. However, there are a number of applications–mostly intranet web applications–that do and don’t work with certain browsers. And unfortunately for me, my company has applications that require Internet Explorer and ones that require anything-but-Internet-Explorer.

Historically, I’ve kept track of various intranet sites by using bookmarks in my browser of choice. The fact that I’m forced to use different browsers to visit different sites throws a wrinkle in the plan, though. So what to do!?

The first step is to create a shortcut for the browser-specific website. The trick is to create a shortcut to the browser executable and pass the URL as a parameter. Follow these steps to create the shortcut:

  1. Right-click on your desktop or in a folder in WIndows Explorer and choose New > Shortcut
  2. For the “location of the item,” enter the path to your browser’s executable followed by the URL. Example: “C:\Program Files\Internet Explorer\iexplore.exe” http://www.msn.com

Using this technique, I can create shortcuts that launch different browsers and keep them in a folder on my desktop. By putting them into a common folder, I can keep all my shortcuts in one place. Of course, the annoying part of doing this is that I’ll have some work to do as browsers and applications evolve. I’ll need to manually update shortcuts if an install path changes or an application no longer needs the specific browser. But hey–it’s an imperfect solution to solve a problem caused by imperfect application.

%d bloggers like this: