How to use Console.WriteLine in ASP.NET (C#) during debug? How to use Console.WriteLine in ASP.NET (C#) during debug? asp.net asp.net

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


Console.Write will not work in ASP.NET as it is called using the browser. Use Response.Write instead.

See Stack Overflow question Where does Console.WriteLine go in ASP.NET?.

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

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

but this will work only during debug.

See Stack Overflow question Debug.WriteLine not working.


using System.Diagnostics;

The following will print to your output as long as the dropdown is set to 'Debug' as shown below.

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


enter image description here


If for whatever reason you'd like 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; }    }}