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!


Jonathan said...

Hey Liuer,

Great post. Love NAnt in color.
Good to see you back into the blogging world.

Looking forward to hearing some more dev tips and tricks from a top coder such as yourself.

Cheers - JR

Anonymous said...

It also works with nant-0.86 beta 1.

Tom said...

Great!! I hope that you are working to get this integrated into the actual Nant codebase. I'll hope to see this in 0.86! Thanks, Tom

Albert Ritmeester said...

For people who doesn't want to compile the code. I have also created a colorizer for nant. The projects homepage is at http://colornant.googlepages.com/

Albert Ritmeester

Pat said...

Is it correct that it only needs .NET 2.0 or higher to work? Coding isn't my strong suit so applogize if this is a silly question.

Bealer said...

Don't suppose you could put the compiled dll up on your blog. It's not the easiest thing to have to create.

If not I'll give it another try tomorrow.

Bealer said...

Ah no worries, got it working. Excellent post, now our "go.bat" output looks so much better.

mandersen said...

how bout a frickin blog entry once in a while? :( :'D :::**&?

Anonymous said...

Hi Eric,

I have created a NAnt extension based on your NAnt color patch.

Please have a look at http://nantloggerext.sourceforge.net/

Best regards


Offshore software development company said...

Thanks for sharing this article on Software development. It was very nice.

Looking for more..................Please continue.

Android Application Development said...

Hello,great post. Information are pretty exciting and saved me huge amount of time which I have spend on something else instead of searching posts like this. I am waiting for more.

Roger said...

Hi there,

Sorry for posting here - couldn't find your e-mail address.

I'm the leading editor of Software Developer's Journal which is a new e-magazine. I'm looking for developers who would like to share their knowledge with other developers. If you are interested in cooperation (articles writing, proofreading, ads) just please let me know - roger.zacharczyk@software.com.pl

Mike Bosch - Software Engineer said...

Very valuable post. Thanks for sharing.

Jethro123 said...

Nice post. This is exactly what I've been looking for. Thanks.

wernight said...

How about submitting it to NAnt so that it gets built in? Sadly it's still on SF and not something like GitHub but should still be simple given you've a patch.

Software Development Melbourne said...

Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.