Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Console.WriteLine go in Debug?

I found this question, but what I want to know is different - does the output from Console.WriteLine go anywhere when debugging? I know that for it to go to the output window I should should Debug.WriteLine() or other methods, but where does the standard Console.WriteLine() go?

Edit When debugging, you don't see the black console window / test log - so the real question is how can I access/view this output during debugging?

like image 804
ripper234 Avatar asked Oct 30 '08 14:10

ripper234


People also ask

Where does console WriteLine go to?

Console. writeline() goes to a console window: the black command / dos prompt.

What is Debug WriteLine in C#?

WriteLine(String)Writes a message followed by a line terminator to the trace listeners in the Listeners collection. public: static void WriteLine(System::String ^ message); C# Copy. [System.Diagnostics.Conditional("DEBUG")] public static void WriteLine (string message); C# Copy.

Where is the console in Visual Studio?

Press F11 . Visual Studio calls the Console.

How do you get the console WriteLine output code in Visual Studio?

It will show your output if you will press ctrl+F5. You will get the output in console window.


2 Answers

The console can redirect it's output to any textwriter. If you implement a textwriter that writes to Diagnostics.Debug, you are all set.

Here's a textwriter that writes to the debugger.

using System.Diagnostics; using System.IO; using System.Text;  namespace TestConsole {     public class DebugTextWriter : TextWriter     {         public override Encoding Encoding         {             get { return Encoding.UTF8; }         }          //Required         public override void Write(char value)         {             Debug.Write(value);         }          //Added for efficiency         public override void Write(string value)         {             Debug.Write(value);         }          //Added for efficiency         public override void WriteLine(string value)         {             Debug.WriteLine(value);         }     } } 

Since it uses Diagnostics.Debug it will adhere to your compiler settings to wether it should write any output or not. This output can also be seen in Sysinternals DebugView.

Here's how you use it:

using System;  namespace TestConsole {     class Program     {         static void Main(string[] args)         {             Console.SetOut(new DebugTextWriter());             Console.WriteLine("This text goes to the Visual Studio output window.");         }     } } 

If you want to see the output in Sysinternals DebugView when you are compiling in Release mode, you can use a TextWriter that writes to the OutputDebugString API. It could look like this:

using System.IO; using System.Runtime.InteropServices; using System.Text;  namespace TestConsole {     public class OutputDebugStringTextWriter : TextWriter     {         [DllImport("kernel32.dll")]         static extern void OutputDebugString(string lpOutputString);          public override Encoding Encoding         {             get { return Encoding.UTF8; }         }          //Required         public override void Write(char value)         {             OutputDebugString(value.ToString());         }          //Added for efficiency         public override void Write(string value)         {             OutputDebugString(value);         }          //Added for efficiency         public override void WriteLine(string value)         {             OutputDebugString(value);         }     } } 
like image 120
Carl R Avatar answered Oct 29 '22 02:10

Carl R


NullStream, which is defined as "A Stream with no backing store.". All the methods do nothing or return nothing. It is an internal class to Stream. The following code is taken from Microsoft's source code.

Basically, when one of the Console write methods is call the first time, a call is made to the Windows API function GetStdHandle for "standard output". If no handle is returned a NullStream is created and used.

Samuel's answer is correct and provides general information. To actually redirect Console output, regardless of the project type, use Console.SetOut(New System.IO.StreamWriter("C:\ConsoleOutput.txt")), which is a simple example.

Directing Console, Debug, and Trace to File

To answer your question directly. Use the ConsoleTraceListener and a StreamWriter to direct all three outputs to a file. I use the following for development only.

    Dim oLogFile As New System.IO.StreamWriter("C:\ConsoleOutput.txt")     oLogFile.AutoFlush = True 'so we do not have to worry about flushing before application exit      Console.SetOut(oLogFile)      'note, writing to debug and trace causes output on console, so you will get double output in log file     Dim oListener As New ConsoleTraceListener     Debug.Listeners.Add(oListener)     Trace.Listeners.Add(oListener) 

NullStream

[Serializable] private sealed class NullStream : Stream {     internal NullStream() { }      public override bool CanRead {         get { return true; }     }      public override bool CanWrite {         get { return true; }     }      public override bool CanSeek {         get { return true; }     }      public override long Length {         get { return 0; }     }      public override long Position {         get { return 0; }         set { }     }      // No need to override Close      public override void Flush() {     }      public override int Read([In, Out] byte[] buffer, int offset, int count) {         return 0;     }      public override int ReadByte() {         return -1;     }      public override void Write(byte[] buffer, int offset, int count) {     }      public override void WriteByte(byte value) {     }      public override long Seek(long offset, SeekOrigin origin) {         return 0;     }      public override void SetLength(long length) {     } }  
like image 27
AMissico Avatar answered Oct 29 '22 03:10

AMissico