From a Console Application project in Visual Studio, I want to redirect Console
's output to the Output Window while debugging.
views:
1436answers:
4
+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
2010-03-25 19:00:33
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
2010-03-25 19:42:21
How does the output differ?
dkackman
2010-03-25 20:12:40
I do not remember specifically, but I was missing new lines. Probably because Console.WriteLine() use.
AMissico
2010-03-25 20:48:48
There was a bug where the WriteLine overload was using Debug.Write instead of Debug.WriteLine. I've edited.
dkackman
2010-03-25 21:01:29
+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
2010-03-25 19:24:08
Ah, that's right. I knew it was something easy. I must have done this at least five times.
AMissico
2010-03-25 19:39:18
Moreover, don't forget to remove the System.Windows.Forms reference that is added when changing the project type.
AMissico
2010-03-27 16:53:52
"Don't forget to reset application back to Console type after debugging." I just did. LOL
AMissico
2010-03-27 18:19:08