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

こ雲淡風輕ζ 提交于 2019-12-01 06:15:47
akton

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.

I don't think you can. A responsible developer may write information from his code to help debugging, but always Trace.Write or Debug.Write, never Console.Write.

MatthewMartin

The accepted answer is correct. But if you want to pound nails into the coffee table with your shoe (a wrong goal and a wrong tool), then you would need to stop iis, and run it interactively from the console, by logging into the server by remote desktop and launching inetinfo.exe with suitable switches from a cmd.exe prompt. Here is onea MSDN page that illustrates the technique for older versions of IIS: http://msdn.microsoft.com/en-us/library/aa291261(v=vs.71).aspx I suppose it still works in principle for newer versions of IIS.

If you don't want to use a library, you can always use System.Diagnostics TraceSource, and then redirect your trace to ConsoleTraceListeners in development (don't forget to attach a console) and then to file or database listeners on server environments.

You can grab the output of Console.WriteLine() of a C# / .Net compiled application running on IIS by using a DebugView from there: https://technet.microsoft.com/en-us/sysinternals/bb896647

use MessageBox,it helps lot.
whatever u want to observe put in messagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!