100

I want to write some result to the console in ASP.NET (C#). It works in a Window application, but a Web application does not work. Here is what I have 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 see nothing in the Output panel. How do I solve this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leap Bun
  • 2,265
  • 5
  • 28
  • 44

7 Answers7

207

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.

Community
  • 1
  • 1
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
  • 6
    Response.Write will write to the http response stream ,I dont think @Leap Bun wants that – labroo Mar 08 '12 at 07:35
  • @labroo Of course. Respone.Write will display text to browser. I can't use it. – Leap Bun Mar 08 '12 at 07:38
  • 1
    @PraveenVenu I already tried System.Diagnostics.Debug.WriteLine("SomeText"); in the question! – Leap Bun Mar 08 '12 at 07:39
  • 2
    Note that you can change the output of System.Diagnostics.Debug by manipulating the Listeners collection. See [MSDN](http://msdn.microsoft.com/en-us/library/system.diagnostics.consoletracelistener.aspx) for more info – T. Fabre Mar 08 '12 at 07:40
  • Is your Output window is in Debug mode? and in web.config? – PraveenVenu Mar 08 '12 at 07:50
  • All I see is a bunch of Application Insights Telemetry crap. My debug messages don't show up. – Tyler Jul 20 '16 at 08:30
33

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

cillierscharl
  • 7,043
  • 3
  • 29
  • 47
10

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; }
    }
}
David
  • 3,736
  • 8
  • 33
  • 52
2

Trace.Write("Error Message") and Trace.Warn("Error Message") are the methods to use in web, need to decorate the page header trace=true and in config file to hide the error message text to go to end-user and so as to stay in iis itself for programmer debug.

1

You shouldn't launch as an IIS server. check your launch setting, make sure it switched to your project name( change this name in your launchSettings.json file ), not the IIS.

enter image description here

Guokas
  • 750
  • 7
  • 23
0

Use response.write method in the code-behind.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-2

Make sure you start your application in Debug mode (F5), not without debugging (Ctrl+F5) and then select "Show output from: Debug" in the Output panel in Visual Studio.

Yuankun
  • 6,875
  • 3
  • 32
  • 34
phn
  • 1,720
  • 1
  • 14
  • 10