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

痞子三分冷 提交于 2019-12-01 03:48:25

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/12544480/where-does-console-writeline-go-in-asp-net-production-environment

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