AMissico
AMissico

Reputation: 21684

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

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

Upvotes: 34

Views: 29131

Answers (6)

ivanatpr
ivanatpr

Reputation: 1880

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; }
    }
}

Upvotes: 3

Tidjani
Tidjani

Reputation: 1

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

Upvotes: -2

dkackman
dkackman

Reputation: 15579

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

Upvotes: 19

Alex F
Alex F

Reputation: 43331

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.

Upvotes: 31

TobyEvans
TobyEvans

Reputation: 1461

Try Trace.Write and use DebugView

Upvotes: 0

ken1nil
ken1nil

Reputation: 935

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

Upvotes: 5

Related Questions