![](/img/trans.png)
[英]Is there a TextWriter interface to the System.Diagnostics.Debug class, for use with Console.SetOut?
[英]Is there a TextWriter interface to the System.Diagnostics.Debug class?
我经常对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,还是应该自己滚动?
Debug.Print函数允许您使用格式和参数。
如果您更喜欢使用TextWriter接口,请使用以下包装类:
public class DebugTextWriter : StreamWriter
{
public DebugTextWriter()
: base(new DebugOutStream(), Encoding.Unicode, 1024)
{
this.AutoFlush = true;
}
sealed 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 => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override void Flush() => Debug.Flush();
public override long Length => throw bad_op;
public override int Read(byte[] buffer, int offset, int count) => throw bad_op;
public override long Seek(long offset, SeekOrigin origin) => throw bad_op;
public override void SetLength(long value) => throw bad_op;
public override long Position
{
get => throw bad_op;
set => throw bad_op;
}
static InvalidOperationException bad_op => 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
你特别需要一个完整的TextWriter
吗? 虽然这有点“快速和肮脏”,但我怀疑只有几种方法的静态类可以很好地完成:
public static class DebugEx
{
[Conditional("DEBUG")]
public static void WriteLine(string format, params object[] args)
{
Debug.WriteLine(string.Format(format, args));
}
}
或类似的东西。
请注意,我个人会看一下像log4net这样的东西,以便更好地控制输出。
对于C#6及更高版本, 字符串插值是大多数字符串格式的答案。 所以这个例子看起来像:
Debug.WriteLine($"# entries {countOfEntries} for connection {connection}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.