Redirect Console.Write… Methods to Visual Studio's Output Window While Debugging

前端 未结 7 1404
死守一世寂寞
死守一世寂寞 2020-12-03 04:47

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

7条回答
  •  失恋的感觉
    2020-12-03 05:05

        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 :) **

提交回复
热议问题