34

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

AMissico
  • 21,470
  • 7
  • 78
  • 106
  • Possible duplicate of [Capturing cout in Visual Studio 2005 output window?](https://stackoverflow.com/questions/73286/capturing-cout-in-visual-studio-2005-output-window) – mr NAE Feb 01 '19 at 07:58

6 Answers6

31

Change application type to Windows before debugging. Without Console window, Console.WriteLine works like Trace.WriteLine. Don't forget to reset application back to Console type after debugging.

Alex F
  • 42,307
  • 41
  • 144
  • 212
19
    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 :) **

dkackman
  • 15,179
  • 13
  • 69
  • 123
  • The problem with this approach is 1) it is code based 2) the output is not the same, and 3) a console window is still opened. – AMissico Mar 25 '10 at 19:42
  • I do not remember specifically, but I was missing new lines. Probably because Console.WriteLine() use. – AMissico Mar 25 '10 at 20:48
  • 1
    There was a bug where the WriteLine overload was using Debug.Write instead of Debug.WriteLine. I've edited. – dkackman Mar 25 '10 at 21:01
  • I think it is best to override `public override void Write(char value)` as it seems to be called from all variations of Write, WriteLine, etc. Inside it, only need `Debug.Write(value);` as the base function doesn't do anything. – Mafu Josh Feb 11 '19 at 16:12
5

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

ken1nil
  • 915
  • 3
  • 9
  • 21
3

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; }
    }
}
ivanatpr
  • 1,862
  • 14
  • 18
0

Try Trace.Write and use DebugView

TobyEvans
  • 1,431
  • 2
  • 21
  • 27
-2

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