Click here to Skip to main content
Click here to Skip to main content
Go to top

Logging: How to Growl with NLog 3 (or 2.1)

, 15 Jun 2014
Rate this:
Please Sign up or sign in to vote.
This will set you logging from your NLog to your Growl in no time.

Demo -> Visual Studio solution (with (445kb) and without (29kb) executables).

"growl adapter for nlog 3.zip" -> The 3 files you need in order to be able to connect NLog to Growl.

"nlog 3.0.0.zip" -> the NLog dll, you can get it from here, NuGet, or their homepage.

"NLog config and schema.zip" -> You can download and add these to your project (and save some time), or either create the config manually (you'll need this), and get the schema from NuGet (optional, but recommended).

Introduction

In this article I'll walk you through installing configuring and successfully logging messages from NLog to Growl.

If you haven't worked with logging before, I highly suggest having a look at my other article first:

If you don't know what Growl is, here's growl in a nutshell (taken from the Growl homepage) :

  • Growl for Windows is a Windows-compatible version of Growl, a notification system for Mac OS X.

  • Put simply, Growl lets you know when things happen. Files finished downloading, friends came online, new email has arrived - Growl can let you know when any event occurs with a subtle notification. The rest of the time, Growl stays out of your way.

Background

This article is here because what I've found on the web didn't work. The original idea and example (as far as I could find) is credited to Ryan Farly, and can be found in the following article:

Trying to use the above links and tutorial, led me down a path of frustration since nothing seemed to work. The reason is that at the time that the above was written NLog was using the .NET 2 framework.

Growl is compiled with the .NET 2 framework as well, and hence the libraries for the targets were compiled with the same targets.

As much as I love NLog, their webpage claimed that NLog 3 is coming soon (2013-12-14), and it was around late May that I've struggled with this. The NuGet package manager installed NLog 3 without saying anything, and I've assumed I'm using 2.1. In the last NLog, support for .NET 2.0 was dropped, and hence why nothing would work when I was trying to log. (It seems that less than a week ago the NLog website was updated, announcing that NLog 3 has been release, but I can't find the download in their download page. Head to NuGet, or get it from my files above).

So, in order to save everybody the pain, here's the updated files you need, plus a tutorial about how to get this up and running in less than 10 minutes.

Using the code

If you know all about NLog, and just want the steps to get this working, jump over to this section, otherwise, keep reading.

Step 1: Get your system ready

1.1 Download and install Growl.

You can find the installer on their main page. Make sure Growl is running if you're trying to use it as a target for your logging.

1.2 Get NLog:

Either from their download page, from the above zip, or from NuGet.

Step 2: Create a console project in VS:

Nothing fancy, all it needs to do is serve as a starting point. The following is more than enough:

using System;

namespace GrowlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Out.WriteLine("I feel like Growling");

            Console.Out.WriteLine("Hit Enter to quit");
            Console.In.ReadLine();
        }
    }
}

Build and run this to see that everything is working. You should see the text on the screen.

Step 3: Adding basic logging to our project:

Add a reference to the NLog.dll (if you've installed it through NuGet, it's already done for you).

Add the files from the Nlog config and schema.zip to your project. It should look like:

The NLog schema (NLog.xsd) will enable intellisense for your NLog.config, and the later will hold the target and the rules for your logging. Note: This can also be done programmatically, and it is part of another article than I'm going to write in the near future.

Your NLog.config should look like:
 

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
  <targets>
    <target name="console"
            xsi:type="ColoredConsole"
            layout="${shortdate} ${level} ${message}"
    />
 
  <rules>
    <logger name="*"
            minlevel="Trace"
            writeTo="console" />

  </rules>
</nlog>

This will simply write the log to a colored console for now. Modify your main method to look like this:

 static void Main(string[] args)
        {
            Console.Out.WriteLine("Growl");

            Logger logger = LogManager.GetCurrentClassLogger();

            logger.Trace("A trace");
            logger.Debug("A trace");
            logger.Info("A Info");
            logger.Warn("A Warn");
            logger.Error("A Error");
            logger.Fatal("A Fatal");

            // Wait for enter to quit
            Console.Out.WriteLine("Hit Enter to quit");
            Console.In.ReadLine();
        }

This will add some logging that we can easily see.

Note: If you try to run this at this point, it will NOT work. The reason is that configuration file for NLog by default is set to "Do not copy". Select the config file, and in the properties change the "Copy to output directory" to "Copy always". Here's a screenshot:

If you'll build and run your project, you should see something like the following:

Step 4: Growling

The simplest way to get growling would be to add the targets to the NLog config file, add the dll's to the output directory, and wualla, you're good to go.

So first, modify your NLog.config to look like:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <extensions>
        <add assembly="NLog.Targets.GrowlNotify" />
    </extensions>

    <targets>
        <target name="console"
                xsi:type="ColoredConsole"
                layout="${shortdate} ${level} ${message}"         
        />

        <target name="growl"
            xsi:type="GrowlNotify"
            password=""
            host=""
            port="23053"         
        />
    </targets>

    <rules>
        <logger name="*"
            minlevel="Trace"
            writeTo="console" />

        <logger name="*"
                minlevel="Trace"
                writeTo="growl"/>
    </rules>
</nlog>

Basically all we've done is added a target to our logger, and a rule for that target.

Since the target is not part of the normal targets, we're also including the NLog.Targets.GrowlNotify.dll by importing the assembly.

Second, copy the files in the dll's from the above "growl adapter for nlog 3.zip" into your program directory (\bin\debug by default). Note: you won't see any logging happening if you skip this step. Later on I'll discuss some other options to achieve the same.

Third, compile and run your program, you should also see something like:

Congratulations, you've successfully logged from NLog to Growl. From here on, the sky's the limit, and it's all up to your imagination.

Jump to the pitfalls if you have any problems.

The very short basics-only things you need to do to NLog to Growl:

1. You'll need to have NLog referenced in your project.

2. You'll need to have the 3 dll's from the  "growl adapter for nlog 3.zip" in your output directory.

3. You'll need to create the target by using a config file, or doing it programmatically.

Common pitfalls

The following are things that might save you hours of trying to figure out what's wrong:

1. Missing dll's:

If you didn't copy the 3 files from the adapter zip to the output directory, or if your config file is not set to "copy always", or if your NLog dll is not referenced ... You'll see no logging.

2. Bad Growl target configuration:

If you're missing the following port="23053" in your growl target configuration, you'll see no logging.

3. Multiple NLog.config files

If you're using multiple configuration files, only one will end up in the output directory. If it's the right one, you're lucky and you'll see your logging, otherwise ... you know ... gurnisht ...

4. Multiple NLog.xsd files

Having multiple of those will throws annoying warnings and break the intellisense. Make sure you have only one referenced, or go to Menu -> XML -> Schemas, and make sure only 1 is selected.

Alternatives

Use Ryan's project:

You can fetch the original project from Ryan Farley's Github page here.

Once you do, you can add it to your project, and either reference it directly, or play around with it. Whatever you do, make sure it's not compiled as a .NET 2.0 project Smile | :) .

Configure targets programmatically:

Another thing you can do is define the targets in code instead of in a config file. The code shall look like this :

// 1.  create configuration object
var config = new LoggingConfiguration();

//  2.  create targets and add them to the config
var console_target = new ColoredConsoleTarget();
config.AddTarget("console", console_target);

var growl_target = new NLog.Targets.GrowlNotify();
config.AddTarget("growl", growl_target);

//  3.  set target propeties
console_target.Layout = "${date:format=HH\\:MM\\:ss.fff} | ${level:uppercase=true}\t| ${logger} | ${message}";
growl_target.Port = 23053;

//  4.  define rules
var rule_1 = new LoggingRule("*", LogLevel.Trace, console_target);
config.LoggingRules.Add(rule_1);

var rule_2 = new LoggingRule("Test.*", LogLevel.Trace, growl_target);
config.LoggingRules.Add(rule_2);

//  5.  activate the configuration
LogManager.Configuration = config;

This will achieve the same as the above config file. It has benefits and disadvantages, but this is part of the next article, so keep tune if you're interested in the subject.

Points of Interest

As you can see, there are many ways to skin the cat. Please note the common pitfalls, and make sure you have a hand rubber ducky for when things don't work.

For more on logging and the different levels, have a look at my NLog article, or at this Wiki page.

If you've found this article helpful, please vote for the article, leave a message, and feel free to post back to this article Smile | :)

History

  • 15 June, 2014: Initial release.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

_Noctis_
Software Developer
Australia Australia
Coding since I Remeber myself ... went through Basic on Commodore 64 to C# on an 8 core i7 ... In between worked with c, c++, java, assembler, php, pascal, JScript, SQL based DB's and a bit of NoSQL as well.
 
Love software, and I'm usually fidgeting around with technology software and hardware on my free time.
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 15 Jun 2014
Article Copyright 2014 by _Noctis_
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid