Tuesday, March 25, 2008

Colorized NAnt Console Output

If you use NAnt for your build scripts, colored console output may be helpful in quickly spotting warnings or errors. I recently wrote a logger to do that:

  1. Download the source for the version of NAnt you're using (I tested this against NAnt 0.85).
  2. Apply this patch to the source tree. If you're on Windows, I believe you can use 'patch.exe' which comes with UnxUtils.
  3. Build NAnt by following the instructions in 'README.txt'. This will produce a bunch of files, but the only one we're interested in is 'NAnt.Core.dll'.
  4. In the directory where you store your NAnt binaries (eg. on my current project, it's in MyCurrentProject/tools/nant), replace 'NAnt.Core.dll' with the new one.
  5. The last step is to add an option to your 'nant' call to specify the use of the new logger:
    nant -logger:NAnt.Core.ConsoleColorLogger ...
    On my current project, we have a 'build.bat' which invokes NAnt, so I just added the option there.

Once you have this in place, you should start seeing colors when a warning or error occurs. Feel free to use this sample build script to test it out (remember to invoke NAnt with -debug in order to see the debug-level messages!).

Customizing the colors

Simply specify additional properties when you invoke NAnt like this:

nant -logger:NAnt.Core.ConsoleColorLogger -D:ConsoleColorLogger.error=DarkRed -D:ConsoleColorLogger.warning=DarkMagenta ...

The levels you can customize are one of error/warning/info/verbose/debug. The colors you can use are 'default' (which means to use the foreground color you had before invoking NAnt) or one of these.

Hope that helps!