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

我经常对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,还是应该自己滚动?

你特别需要一个完整的TextWriter吗? 虽然这有点“快速和肮脏”,但我怀疑只有几种方法的静态类可以很好地完成:

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

或类似的东西。

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

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(); } } }; } 

以下是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