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

跟風遠走 提交于 2019-11-29 13:19:23
Avram

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());
Carl G

@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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!