tags:

views:

103

answers:

1

I'm often frustrated by the System.Diagnostics.Debug.Write/WriteLine methods. I would like to use the Write/WriteLine methods familiar from the TextWriter class, so I often write

Debug.WriteLine("# entries {0} for connection {1}", countOfEntries, connection);

which causes a compiler error. I end up writing

Debug.WriteLine(string.Format("# entries {0} for connection {1}", 
    countOfEntries, connection));

which is really awkward.

Does the CLR have a class deriving from TextWriter that "wraps" System.Debug, or should I roll my own?

+1  A: 

Do you particularly need a whole TextWriter? While this is somewhat "quick and dirty" I suspect a static class with just a few methods would do perfectly well:

public static class DebugEx
{
    [Conditional("DEBUG")]
    public static void WriteLine(string format, params object[] args)
    {
        Debug.WriteLine(string.Format(format, args);
    }
}

or something similar.

Mind you, I'd personally look at something like log4net to give more control over the output.

Jon Skeet