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

6

7

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

Byrom answered 22/9, 2012 at 14:11 Comment(1)
very similar to #10747328 see my answer to that question.Conflation
C
14

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.

Coh answered 22/9, 2012 at 14:26 Comment(0)
A
3

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.

Alenealenson answered 22/9, 2012 at 14:28 Comment(1)
excellent call on Debug.WriteLine. I was just trying to get some quick timings on my IIS app.Franklyn
U
1

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.

Uncrown answered 22/9, 2012 at 14:17 Comment(2)
Dear Danny, Will there be any chance to catch the output of Trace.Write and Debug.Write after deploying the web app in IIS (e.g. in a system-defined log file or windows event viewer or somewhere universally accessible?)Byrom
+1 Trace.Write or Debug.Write is the way it should be (when not using a logger).Jsandye
K
1

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.

Kneepan answered 22/9, 2012 at 20:11 Comment(1)
Thanks Matthew. Your solution also is considerable.Byrom
H
1

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

Hephzipah answered 8/1, 2016 at 15:36 Comment(0)
B
0

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

Bianka answered 22/9, 2012 at 14:22 Comment(1)
Dear Ravindra. It is actually a SOAP web service deployed in IIS, not a winform app.Byrom

© 2022 - 2024 — McMap. All rights reserved.