是否有一个的TextWriter接口System.Diagnostics.Debug类?有一个、接口、TextWriter、Debug

2023-09-03 01:20:15 作者:爬进棺材调戏鬼

我经常被System.Diagnostics.Debug.Write /的WriteLine方法受挫。我想用写/的WriteLine方法从的TextWriter类的熟悉,所以我经常写

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.

是否CLR有自TextWriter类派生的包装System.Debug,或者我应该推出自己的?

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

推荐答案

你特别需要一个整体的TextWriter ?虽然这多少有些快速和肮脏我怀疑是静态类,只有几个方法会做得很清楚:

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.

你要知道,我会亲自考察一下log4net的给予更多的控制输出。

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