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

时间:2010-05-06 08:54:07

标签: c# .net

我经常对System.Diagnostics.Debug.Write / WriteLine方法感到沮丧。我想使用TextWriter类中熟悉的Write / WriteLine方法,所以我经常写

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

导致编译器错误。我最后写了

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

这真的很尴尬。

CLR是否有一个派生自TextWriter的类“包装”System.Debug,还是应该自己滚动?

4 个答案:

答案 0 :(得分:32)

函数Debug.Print允许您使用格式和参数。

[编辑:]

如果您更喜欢使用 TextWriter 界面,则可以使用以下内容:

public class DebugTextWriter : StreamWriter
{
    public DebugTextWriter()
        : base(new DebugOutStream(), Encoding.Unicode, 1024)
    {
        this.AutoFlush = true;
    }

    class DebugOutStream : Stream
    {
        public override void Write(byte[] buffer, int offset, int count)
        {
            Debug.Write(Encoding.Unicode.GetString(buffer, offset, count));
        }

        public override bool CanRead { get { return false; } }
        public override bool CanSeek { get { return false; } }
        public override bool CanWrite { get { return true; } }
        public override void Flush() { Debug.Flush(); }
        public override long Length { get { throw new InvalidOperationException(); } }
        public override int Read(byte[] buffer, int offset, int count) { throw new InvalidOperationException(); }
        public override long Seek(long offset, SeekOrigin origin) { throw new InvalidOperationException(); }
        public override void SetLength(long value) { throw new InvalidOperationException(); }
        public override long Position
        {
            get { throw new InvalidOperationException(); }
            set { throw new InvalidOperationException(); }
        }
    };
}

答案 1 :(得分:15)

快速了解System.Diagnostics.Debug的TextWriter包装器:

class TextWriterDebug : System.IO.TextWriter
{
    public override System.Text.Encoding Encoding
    {
        get { return System.Text.Encoding.Default; }
    }

    //public override System.IFormatProvider FormatProvider
    //{ get; }
    //public override string NewLine
    //{ get; set; }

    public override void Close()
    {
        System.Diagnostics.Debug.Close();
        base.Close();
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }

    public override void Flush()
    {
        System.Diagnostics.Debug.Flush();
        base.Flush();
    }

    public override void Write(bool value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(char value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(char[] buffer)
    {
        System.Diagnostics.Debug.Write(buffer);
    }

    public override void Write(decimal value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(double value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(float value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(int value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(long value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(object value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(string value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(uint value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(ulong value)
    {
        System.Diagnostics.Debug.Write(value);
    }

    public override void Write(string format, object arg0)
    {
        System.Diagnostics.Debug.Write(string.Format(format, arg0));
    }

    public override void Write(string format, params object[] arg)
    {
        System.Diagnostics.Debug.Write(string.Format(format, arg));
    }

    public override void Write(char[] buffer, int index, int count)
    {
        string x = new string(buffer, index, count);
        System.Diagnostics.Debug.Write(x);
    }

    public override void Write(string format, object arg0, object arg1)
    {
        System.Diagnostics.Debug.Write(string.Format(format, arg0, arg1));
    }

    public override void Write(string format, object arg0, object arg1, object arg2)
    {
        System.Diagnostics.Debug.Write(string.Format(format, arg0, arg1, arg2));
    }

    public override void WriteLine()
    {
        System.Diagnostics.Debug.WriteLine(string.Empty);
    }

    public override void WriteLine(bool value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(char value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(char[] buffer)
    {
        System.Diagnostics.Debug.WriteLine(buffer);
    }

    public override void WriteLine(decimal value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(double value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(float value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(int value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(long value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(object value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(string value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(uint value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(ulong value)
    {
        System.Diagnostics.Debug.WriteLine(value);
    }

    public override void WriteLine(string format, object arg0)
    {
        System.Diagnostics.Debug.WriteLine(string.Format(format, arg0));
    }

    public override void WriteLine(string format, params object[] arg)
    {
        System.Diagnostics.Debug.WriteLine(string.Format(format, arg));
    }

    public override void WriteLine(char[] buffer, int index, int count)
    {
        string x = new string(buffer, index, count);
        System.Diagnostics.Debug.WriteLine(x);

    }

    public override void WriteLine(string format, object arg0, object arg1)
    {
        System.Diagnostics.Debug.WriteLine(string.Format(format, arg0, arg1));
    }

    public override void WriteLine(string format, object arg0, object arg1, object arg2)
    {
        System.Diagnostics.Debug.WriteLine(string.Format(format, arg0, arg1, arg2));
    }

} // Ends class TextWriterDebug 

答案 2 :(得分:8)

你特别需要整个TextWriter吗?虽然这有点“快速和肮脏”,但我怀疑只用几种方法的静态类就能做得非常好:

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

或类似的东西。

请注意,我会亲自查看类似log4net的内容,以便更好地控制输出。

答案 3 :(得分:1)

对于C#6和更高版本,string interpolation是大多数字符串格式的答案。因此,示例如下所示:

Debug.WriteLine($"# entries {countOfEntries} for connection {connection}");