views:

1436

answers:

4

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

+2  A: 
    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
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
How does the output differ?
dkackman
I do not remember specifically, but I was missing new lines. Probably because Console.WriteLine() use.
AMissico
There was a bug where the WriteLine overload was using Debug.Write instead of Debug.WriteLine. I've edited.
dkackman
A: 

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

Niike2
A: 

Try Trace.Write and use DebugView

TobyEvans
+4  A: 

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 Farber
Ah, that's right. I knew it was something easy. I must have done this at least five times.
AMissico
I forgot, Alex Farber, YOUR AWESOME!
AMissico
Moreover, don't forget to remove the System.Windows.Forms reference that is added when changing the project type.
AMissico
"Don't forget to reset application back to Console type after debugging." I just did. LOL
AMissico