代码之家  ›  专栏  ›  技术社区  ›  John Källén

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

  •  27
  • John Källén  · 技术社区  · 14 年前

    我经常对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 回复  |  直到 6 年前
        1
  •  32
  •   Glenn Slayden    6 年前

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

    [编辑:]

    如果你想用 文本编辑器 界面中,可以使用以下命令:

    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(); }
            }
        };
    }
    
        2
  •  15
  •   WaffleSouffle    13 年前

    下面是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 
    
        3
  •  8
  •   MasterMastic    11 年前

    你特别需要一个 TextWriter ?虽然这有点“又快又脏”,但我怀疑只有几个方法的静态类会做得很好:

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

    或者类似的东西。

    请注意,我会亲自查看log4net之类的东西,以便更好地控制输出。

        4
  •  1
  •   Fabian    6 年前

    C 6及更高版本 string interpolation 是大多数字符串格式的答案。所以这个例子看起来像:

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