Where does Console.WriteLine go in ASP.NET?

Viewed 317641

In a J2EE application (like one running in WebSphere), when I use System.out.println(), my text goes to standard out, which is mapped to a file by the WebSphere admin console.

In an ASP.NET application (like one running in IIS), where does the output of Console.WriteLine() go? The IIS process must have a stdin, stdout and stderr; but is stdout mapped to the Windows version of /dev/null or am I missing a key concept here?

I'm not asking if I should log there (I use log4net), but where does the output go? My best info came from this discussion where they say Console.SetOut() can change the TextWriter, but it still didn't answer the question on what the initial value of the Console is, or how to set it in config/outside of runtime code.

14 Answers

If you use System.Diagnostics.Debug.WriteLine(...) instead of Console.WriteLine(), then you can see the results in the Output window of Visual Studio.

There simply is no console listening by default. Running in debug mode there is a console attached, but in a production environment it is as you suspected, the message just doesn't go anywhere because nothing is listening.

Unless you are in a strict console application, I wouldn't use it, because you can't really see it. I would use Trace.WriteLine() for debugging-type information that can be turned on and off in production.

if you happened to use NLog in your ASP.net project, you can add a Debugger target:

<targets>
    <target name="debugger" xsi:type="Debugger"
            layout="${date:format=HH\:mm\:ss}|${pad:padding=5:inner=${level:uppercase=true}}|${message} "/>

and writes logs to this target for the levels you want:

<rules>
    <logger name="*" minlevel="Trace" writeTo="debugger" />

now you have console output just like Jetty in "Output" window of VS, and make sure you are running in Debug Mode(F5).

Mac, In Debug mode there is a tab for the Output. enter image description here

Using console.Writeline did not work for me.

What did help was putting a breakpoint and then running the test on debug. When it reaches the breakpoint you can observe what is returned.

Try to attach kinda 'backend debugger' to log your msg or data to the console or output window the way we can do in node console.

System.Diagnostics.Debug.WriteLine("Message" + variable) instead of Console.WriteLine()

this way you can see the results in Output window aka Console of Visual Studio.

In an ASP.NET application, I think it goes to the Output or Console window which is visible during debugging.

Related