Article Index

A simple color ConsoleTraceListener

Considering how many pre-release Whidbey demos I saw showing off the new color support in Console applications, I'm surprised this wasn't include in the box. I use a System.Diagnostics.TraceSource coupled with the ConsoleTraceListener for output in console apps. Of course, when you have verbose logging enabled, you see a LOT of gray text scroll across the screen, and nothing really stands out. Reminds me of trying to decode NAnt output after living with MSBuild for a while. I did a quick search for "color consoletracelistener" and came across this offering from Mauricio Rojas. I (probably unfairly) dimissed it because it looked much more complicated than I would expect (ok, and because it was in VB).
So I spent 10 minutes throwing this together. It works for every scenario that I need. In other words, I make no claims that it is general purpose solution for you - it may not add color to every type of trace message (depending on which methods you use to write messages). If this doesn't work for you, Mauricio's may be a better bet.

using System;

using System.Diagnostics;

using System.Collections.Generic;

 

namespace FlimFlan.Diagnostics

{

    public class ColorConsoleTraceListener : ConsoleTraceListener

    {

        Dictionary<TraceEventType, ConsoleColor> eventColor = new Dictionary<TraceEventType, ConsoleColor>();

 

        public ColorConsoleTraceListener()

        {

            eventColor.Add(TraceEventType.Verbose, ConsoleColor.DarkGray);

            eventColor.Add(TraceEventType.Information, ConsoleColor.Gray);

            eventColor.Add(TraceEventType.Warning, ConsoleColor.Yellow);

            eventColor.Add(TraceEventType.Error, ConsoleColor.DarkRed);

            eventColor.Add(TraceEventType.Critical, ConsoleColor.Red);

            eventColor.Add(TraceEventType.Start, ConsoleColor.DarkCyan);

            eventColor.Add(TraceEventType.Stop, ConsoleColor.DarkCyan);

        }

 

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)

        {

            TraceEvent(eventCache, source, eventType, id, "{0}", message);

        }

 

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)

        {

            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = getEventColor(eventType, originalColor);

            base.TraceEvent(eventCache, source, eventType, id, format, args);

            Console.ForegroundColor = originalColor;

        }

 

        private ConsoleColor getEventColor(TraceEventType eventType, ConsoleColor defaultColor)

        {

            if (!eventColor.ContainsKey(eventType))

            {

                return defaultColor;

            }

            return eventColor[eventType];

        }

    }

}


You can see it in action using the following app:

using System.Diagnostics;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            TraceSource trace = new TraceSource("log");

            trace.TraceEvent(TraceEventType.Start, 0);

            trace.TraceInformation("Hello World");

            trace.TraceEvent(TraceEventType.Error, 0, "Something failed.");

            trace.TraceEvent(TraceEventType.Verbose, 0, "I like ice cream.");

            trace.TraceEvent(TraceEventType.Critical, 0, "Something went horribly wrong!");

            trace.TraceEvent(TraceEventType.Verbose, 0, "I like cherries.");

            trace.TraceEvent(TraceEventType.Warning, 0, "This program will end soon...");

            trace.TraceEvent(TraceEventType.Information, 0, "Ending program.");

            trace.TraceEvent(TraceEventType.Stop, 0);

        }

    }

}


Add these settings to the configuration section of your app.config:

  <system.diagnostics>

    <sources>

      <source name="log" switchValue="All">

        <listeners>

          <add name="Console" type="FlimFlan.Diagnostics.ColorConsoleTraceListener, ConsoleApplication1" />

        </listeners>

      </source>

    </sources>

  </system.diagnostics>


And you should see something like this: