Question

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

Était-ce utile?

La solution

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.

Autres conseils

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.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top