Redirect console to Visual Studio debug output window in app.config

@Avram's answer has worked for me, except that the single overload in his code wasn't the one that log4net's ConsoleAppender was using on my system. (I'm interested in Console.SetOut so that log4net's ConsoleAppender outputs to Visual Studio's "Debug" output pane.) So I overrode all of StringWriter's Write and WriteLine methods accepting string, object, char[], etc. on the assumption that one or more of these was what ConsoleAppender was calling via Console.

This succeeded, and log4net logging now appears in my "Debug" pane.

I'm including the code below for the benefit of anyone with similar goals. (To be entirely safe, one could override the remaining StringWriter.Write and .WriteLine methods.) I've removed the calls to base because they appear to be unnecessary, and I think they just build up a large buffer inside StringWriter (usually accessed via that class's .ToString().)

namespace GeneralLibrary.Logging
{
    using System.Diagnostics;
    using System.IO;

    public class DebugWriter : StringWriter
    {
        public override void Write(string format, object arg0)
        {
            Debug.Write(string.Format(format, arg0));
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

If you can get hold of the stream for the output window you can use Console.SetOut() to redirect to it. However this approach doesn't appear to be possible.

System.Debug outputs to every TraceListener in its TraceListenerCollection. There is only one TraceListener registered initially which is the DefaultTraceListener. It does not make use of a stream object and instead uses native methods for output.

An approach that uses the Visual Studio API is probably the way to go.


Basically the most simple solution looks like this.

public class ToDebugWriter : StringWriter
{
    public override void WriteLine(string value)
    {
        Debug.WriteLine(value);
        base.WriteLine(value);
    }
}

And you must add this line to the initialization of the program:

Console.SetOut(new ToDebugWriter());