How to use Console.WriteLine in ASP.NET (C #) during debugging?

I want to write some result on the console in ASP.NET (C #). It works in the Window application, but the web application does not work. Here is what I tried:

protected void btonClick_Click(object sender, EventArgs e) { Console.WriteLine("You click me ..................."); System.Diagnostics.Debug.WriteLine("You click me .................."); System.Diagnostics.Trace.WriteLine("You click me .................."); } 

But I don’t see anything in the Output panel. How to solve this problem?

+60
c # visual-studio-2010 console
Mar 08 '12 at 7:30
source share
5 answers

Console.Write will not work in ASP.NET because it is invoked using a browser. Use Response.Write instead.

See stack overflow question. Where is Console.WriteLine included with ASP.NET? .

If you want to write something in the Output window during debugging, you can use

 System.Diagnostics.Debug.WriteLine("SomeText"); 

but this will only work during debugging.

See stack overflow question Debug.WriteLine does not work .

+133
Mar 08 2018-12-12T00:
source share
β€” -

using System.Diagnostics;

The following will print to your output until the drop-down menu is set to "Debug", as shown below.

Debug.WriteLine("Hello, world!");




enter image description here

+22
Mar 08 '12 at 7:40
source share

If for any reason you want to catch the output of Console.WriteLine , you can do this:

 protected void Application_Start(object sender, EventArgs e) { var writer = new LogWriter(); Console.SetOut(writer); } public class LogWriter : TextWriter { public override void WriteLine(string value) { //do whatever with value } public override Encoding Encoding { get { return Encoding.Default; } } } 
+6
Jan 03 '13 at 15:51
source share

Use the response.write method in code-behind .

+2
Sep 15 '14 at 6:22
source share

Trace.Write ("Error message") and Trace.Warn ("Error message") are the methods used on the Internet, you need to decorate the page header trace = true and in the configuration file, go to the end to hide the text of the error message user and stay in iis to debug the programmer.

+1
Jan 15 '14 at 6:10
source share



All Articles