Is there a TextWriter interface to the System.Diagnostics.Debug class?

The function Debug.Print lets you use formatting and arguments.

If you’d prefer to use a TextWriter interface, use the following wrapper class:

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)