Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Console.WriteLine go in ASP.net production environment?

Is there any chance that I can see the output of Console.WriteLine command after deploying my asp.net web application in IIS? (no more Visual Studio)

I checked this question:

Where does Console.WriteLine go in ASP.NET?

But the problem is that they all talk about debugging/development environment which output window of Visual Studio can be used to check the output of those lines.

Is there really a way to view the output of those lines without installing extra logging tools (e.g. log4net)?

like image 521
Farshid Avatar asked Sep 22 '12 14:09

Farshid


People also ask

Where does console WriteLine show up?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them. Set breakpoint before, debug, and then use F11 to step through code line by line.

Where does the console WriteLine () Output go?

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

Where is the console Output in Visual Studio?

Press F11 . Visual Studio calls the Console.

Where does debug WriteLine go C#?

Debug. WriteLine writes to the debug output. You can see it in your debugger and save it from there. It does not write a log file.


2 Answers

Console.WriteLine (which redirects to Console.Out.WrlteLine by default) is written to Stream.Null, meaning things written to them are lost, as per the question you mention.

To redirect Console.Out to another stream, such as a file, use Console.SetOut, such as in the global.asax file BeginRequest handler. This will redirect any Console.WriteLine calls to the output file. Remember to close the stream in the EndRequest handler or similar location.

As others have said here, Console.WriteLine should be generally avoided in a web application or general purpose libraries for this reason. Consider using a logging library, such as log4net.

like image 172
akton Avatar answered Oct 30 '22 20:10

akton


Console.Out by default corresponds to the host process's stdout stream. On Windows only executables marked as being of Console type have their stdout stream directed to a console window - for all other executable types (GUIs and Service processes) then stdout goes nowhere.

ASP.NET runs within w3wp.exe which is a service process without a GUI. As @akton points out it goes to a null stream, so anything written will be lost.

If you want to trace operations for debugging (or rather, post-mortem debugging) then use Debug.WriteLine or use a logging library like log4net.

like image 42
Dai Avatar answered Oct 30 '22 20:10

Dai