views:

3001

answers:

5

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?

+1  A: 

It goes to the console (standard output) or to the stream that the console is set to.

chills42
A: 

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

Joel Coehoorn
A: 

Even in a WinForms app, you can create a console window, but you'll have to go through P/Invoke to call a Win32 method directly. See http://pinvoke.net/default.aspx/kernel32/AllocConsole.html

James Curran
A: 

Debug and Release do not control whether or not you get a console window. That is controlled by the project's output type. (Properties -> Application -> Output Type). Console Application will get you a console window which will visualize and receive input from the window into the Error, In, and Out streams in System.Console.

The System.Console class exposes several properties and methods for interacting with its streams even if you cannot see it. Most notably: Error, In, Out, SetError(), SetIn(), SetOut(), and the Read and Write methods.

Samuel
+1  A: 

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) {
    }
} 
AMissico