2012-03-08 84 views
62

我想写一些结果到ASP.NET(C#)的控制台。 它在Window应用程序中工作,但Web应用程序不起作用。 这是我曾尝试:如何在调试期间在ASP.NET(C#)中使用Console.WriteLine?

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 .................."); 
} 

但我什么也看不到在输出面板。我该如何解决这个问题?

+0

看这里, http://stackoverflow.com/questions/2522099/where-does-debug-write-output-to – labroo 2012-03-08 07:35:27

回答

137

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

请参阅堆栈溢出问题Where does Console.WriteLine go in ASP.NET?

如果你想写调试过程中的东西输出窗口,你可以使用

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

但这只会在调试过程工作。

查看堆栈溢出问题Debug.WriteLine not working

+5

的Response.Write将写入HTTP响应流,我不认为@Leap包子希望那 – labroo 2012-03-08 07:35:08

+0

我已经更新了我的回答 – PraveenVenu 2012-03-08 07:36:25

+0

@labroo当然。 Respone.Write将向浏览器显示文本。我无法使用它。 – 2012-03-08 07:38:20

22

using System.Diagnostics;

下面将只要下拉列表设置为“调试”,如下图所示打印到输出。

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


enter image description here

+12

我以前试过这个,它不起作用! – 2012-03-08 07:48:38

7

如果因任何原因,你想捉的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; } 
    } 
} 
1

Trace.Write(“错误消息“)和Trace.Warn(”错误消息“)是在Web中使用的方法,需要修饰页面标题trace = true并在配置文件中隐藏e rror消息文本发送给最终用户,以便留在iis中供程序员调试。

相关问题