Redirect Console.Write… Methods to Visual Studio&#

2019-01-14 05:51发布

From a Console Application project in Visual Studio, I want to redirect Console's output to the Output Window while debugging.

7条回答
Fickle 薄情
2楼-- · 2019-01-14 06:08

Note if you're using dkackman's method but you want to write the output to BOTH the console window and the debug window, then you can slightly modify his code like this:

class DebugWriter : TextWriter
{
    //save static reference to stdOut
    static TextWriter stdOut = Console.Out;

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

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

    public override Encoding Encoding
    {
        get { return Encoding.Unicode; }
    }
}
查看更多
你好瞎i
3楼-- · 2019-01-14 06:09

Thanks, Alex F, nice solution, but didn't work for me, because my project was created by cmake. So, to do as Alex F suggested, add WIN32 or MACOSX_BUNDLE to add_executable

add_executable(target_name WIN32 <source list>)
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-14 06:18

You can change it to System.Diagnostics.Debug.Write();

查看更多
Melony?
5楼-- · 2019-01-14 06:24

Actually, there is an easiest way: In the "Options" window of Visual Studio (from the Tools menu), go to "Debugging" then check the option "Redirect All Output Window Text to the Immediate Window".

查看更多
祖国的老花朵
6楼-- · 2019-01-14 06:26

Try Trace.Write and use DebugView

查看更多
虎瘦雄心在
7楼-- · 2019-01-14 06:30
    class DebugWriter : TextWriter
    {        
        public override void WriteLine(string value)
        {
            Debug.WriteLine(value);
            base.WriteLine(value);
        }

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

        public override Encoding Encoding
        {
            get { return Encoding.Unicode; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
#if DEBUG         
            if (Debugger.IsAttached)
                Console.SetOut(new DebugWriter());   
#endif

            Console.WriteLine("hi");
        }
    }

** note that this is roughed together almost pseudo code. it works but needs work :) **

查看更多
登录 后发表回答