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 windo
Visual Studio launches Windows programs (/target:winexe) with the stdin/stdout/stderr redirected to Named Pipes. The other end of each pipe is owned by the VS debugger and anything read on stderr/stdout is displayed in the Debug Output Window. Hence, Console.Write
auto-magically appears in the VS Debug output. Note that this does not happen if you attach to an already started process (since the redirect trick can only be done at process launch time).
When launching console programs (/target:exe) this redirect does not occur so the Console.Write
goes tothe actual console (or wherever the stdout
is redirected).
I couldn't find anything that documents this behavior, is just my conclusion from investigating how VS launches and debugs apps.
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
As already commented on OP's question:
No need to write additional code
In Visual Studio uppermost menu choose
Debug > Windows > Output
The output windows will only be visible in debug mode and it will show all e.g.
Console.WriteLine("Debug MyVariable: " + MyVariable)
when you get to them.
Set a breakpoint before (click the different-coloured empty area before the line number at the start of a chosen line), debug (F5), and then step through code line by line (F11) until you do.
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.
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)
[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) {
}
}
It goes to the console (standard output) or to the stream that the console is set to.
The best solution for me was to change Console.WriteLine() to System.Diagnostics.Debug.WriteLine(). For example:
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
Then you can view your errors as an object in the output window.