Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 527
AMissico Avatar asked Mar 25 '10 18:03

AMissico


People also ask

How do I display the Output window or console in Visual Studio?

To display the Output window whenever you build a project, in the Options dialog box, on the Projects and Solutions > General page, select Show Output window when build starts.

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.


2 Answers

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.

like image 134
Alex F Avatar answered Oct 02 '22 10:10

Alex F


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

like image 41
dkackman Avatar answered Oct 02 '22 12:10

dkackman