This question came across the internal conversion alias this morning:

Is there a way to send errors to STDERROR?

This simple little question provides a nice lead-in to a blog entry about custom loggers, so here we go :)

Whenever something interesting happens during the build process, the MSBuild engine raises events to loggers that are attached to the build process. Any number of loggers can be attached to a build, which gives build engineers a great deal of flexibility in how they track the output from the build.

We ship with two built-in loggers: a console logger that shows the build output with pretty colours, and a file logger so you can put all the build spew in a text file. Depending on your build process you might want to take logging a step further. For example, what if you wanted to:

  • log all the build errors and then send an e-mail to build administrators?
  • save build output in a database somewhere for later data mining?
  • send the build errors to STDERROR?

The answer to all of the above is to write a custom logger for MSBuild. Brett, our documentation writer, has some excellent information on writing loggers in our MSDN help files. In particular he has a nice walkthrough that you can use as the basis for your own logger. It's the documentation I used when I wanted to learn more about how logging works. If you want to see a complete example of a custom logger I recommend taking a look at Szymon Kobalczyk's XML Logger. I've actually used this logger in one of my little side projects and found it quite useful.

For the original question, since all the person wanted to do was log errors to a different location, it's probably simplest to subclass from our existing ConsoleLogger rather than writing an entire logger from scratch. Then you can override the Initialize method, add a new event handler to the ErrorRaised event on the event source, and log it as appropriate.

[ Author: Neil Enns ]