Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect console to Visual Studio debug output window in app.config

I want to get my Console.WriteLine() commands to appear in my "Output" window with my Debug.WriteLine() statements. I think I figured out how to do this once, but I can't remember / find on google how to do it again. I seem to remember being able to do this in the app.config file.

I find plenty of instructions on how to get console and debug statements to appear in the output of the Console, but not how to get them to appear in the "Output" window.

Is it possible?

like image 597
Quinn Wilson Avatar asked Sep 10 '09 22:09

Quinn Wilson


People also ask

How do I show console output in Visual Studio?

Press F11 . Visual Studio calls the Console. WriteLine(String, Object, Object) method. The console window displays the formatted string.

How do I write to the Output window in Visual Studio?

You can write run-time messages to the Output window using the Debug class or the Trace class, which are part of the System. Diagnostics class library. Use the Debug class if you only want output in the Debug version of your program. Use the Trace class if you want output in both the Debug and Release versions.

How do I Debug a console application in Visual Studio?

Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin. Press ⌘ ↵ ( command + enter ) to start the program in debugging mode. Another way to start debugging is by choosing Run > Start Debugging from the menu.

How do I keep the console window in Visual Studio?

Start the project with Ctrl + F5 instead of just F5 . The console window will now stay open with the Press any key to continue . . . message after the program exits.


1 Answers

Basically the most simple solution looks like this.

public class ToDebugWriter : StringWriter
{
    public override void WriteLine(string value)
    {
        Debug.WriteLine(value);
        base.WriteLine(value);
    }
}

And you must add this line to the initialization of the program:

Console.SetOut(new ToDebugWriter());
like image 52
Avram Avatar answered Oct 07 '22 17:10

Avram