如何在调试期间在ASP.NET(C#)中使用Console.WriteLine?

Lea*_*Bun 79 c# asp.net console visual-studio-2010

我想在ASP.NET(C#)中将一些结果写入控制台.它适用于Window应用程序,但Web应用程序不起作用.这是我尝试过的:

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 ..................");
}
Run Code Online (Sandbox Code Playgroud)

但我在"输出"面板中看不到任何内容.我该如何解决这个问题?

Pra*_*enu 172

Console.Write在ASP.NET中不起作用,因为它是使用浏览器调用的.请改用Response.Write.

请参阅Stack Overflow问题Console.WriteLine在ASP.NET中的位置?.

如果要在调试期间向Output窗口写入内容,可以使用

System.Diagnostics.Debug.WriteLine("SomeText");
Run Code Online (Sandbox Code Playgroud)

但这只会在调试期间起作用.

请参阅Stack Overflow问题Debug.WriteLine无法正常工作.

  • Response.Write将写入http响应流,我不认为@Leap Bun想要这样 (5认同)
  • 请注意,您可以通过操作 Listeners 集合来更改 System.Diagnostics.Debug 的输出。有关详细信息,请参阅 [MSDN](http://msdn.microsoft.com/en-us/library/system.diagnostics.consoletracelistener.aspx) (2认同)

cil*_*arl 24

using System.Diagnostics;

只要下拉列表设置为"Debug",以下内容将打印到您的输出,如下所示.

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


在此输入图像描述

  • 我以前试过这个,它不起作用! (13认同)

Dav*_*vid 8

如果由于某种原因你想要捕获输出Console.WriteLine,你可以这样做:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)