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)?
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.
It goes to the console (standard output) or to the stream that the console is set to.
Press F11 . Visual Studio calls the Console.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With