C# – Where does Console.WriteLine go in ASP.net production environment

asp.netcconsoleconsole.writelinetrace

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)?

Best Answer

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.