2012-03-08 99 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中供程序員調試。

相關問題