15

Say I have a WCF app hosted in IIS. And in that app I run this line of code:

 Console.WriteLine("Testing, testing 1 2 3");

Where will that be written to? Or is it ignored and just lost?

Is there someway to capture it when needed?

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • 1
    If it's hosted in IIS, then it's the same as ASP.NET, so this post should answer it for you http://stackoverflow.com/questions/137660/where-does-console-writeline-go-in-asp-net - I'd recommend using System.Diagnostics.Debug.WriteLine instead, you have options to where that goes. – Matt Roberts Jan 25 '12 at 20:29

1 Answers1

30

Nowhere. More specifically:

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.

quoted from here: https://stackoverflow.com/a/2075892/12744

actually, the same answer goes to on to address the second part of your question too:

To actually redirect Console output, regardless of the project type, use

  Console.SetOut(New System.IO.StreamWriter("C:\ConsoleOutput.txt")),
Community
  • 1
  • 1
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
  • 2
    The second part of this answer is a gem - it answers "How do I see Console output in an IIS Web app". NOTE: I found that I had to add "Console.Out.Flush();" after every Console.WrriteLine... call to get the output to show up in the file as soon as the WriteLine call is made. – BRebey Nov 18 '20 at 02:49
  • 2
    There is just missing that part: "writer.AutoFlush = True" – Marcus.D Jan 06 '21 at 13:53