From a Console Application project in Visual Studio, I want to redirect Console
's output to the Output Window while debugging.
Asked
Active
Viewed 2.8k times
34

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 Answers
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
-
Ah, that's right. I knew it was something easy. I must have done this at least five times. – AMissico Mar 25 '10 at 19:39
-
-
Moreover, don't forget to remove the System.Windows.Forms reference that is added when changing the project type. – AMissico Mar 27 '10 at 16:53
-
1"Don't forget to reset application back to Console type after debugging." I just did. LOL – AMissico Mar 27 '10 at 18:19
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
-
1There 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
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
-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".

Tidjani
- 1
-
ctrl-alt-I brings up the intermediate window, but yeah it shows as empty. – barlop Aug 02 '14 at 07:04