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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9614218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:10:31  来源:igfitidea点击:

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

c#asp.netvisual-studio-2010console

提问by Leap Bun

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:

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

But I see nothing in the Output panel. How do I solve this problem?

但是我在“输出”面板中什么也没看到。我该如何解决这个问题?

采纳答案by PraveenVenu

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

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

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

请参阅堆栈溢出问题Console.WriteLine 在 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.

请参阅堆栈溢出问题Debug.WriteLine not working

回答by cillierscharl

using System.Diagnostics;

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!");

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



enter image description here

在此处输入图片说明

回答by David

If for whatever reason you'd like to catch the output of Console.WriteLine, you CAN do this:

如果出于某种原因您想捕获 的输出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; }
    }
}

回答by DeepakJoseLopez

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.

Trace.Write("Error Message") 和 Trace.Warn("Error Message") 是web中使用的方法,需要修饰页眉trace=true,并在配置文件中隐藏错误信息文本去结束-user 等,以便留在 iis 本身以供程序员调试。

回答by albin.varghese

Use response.write method in the code-behind.

代码隐藏中使用 response.write 方法。

回答by phn

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.

确保在调试模式 ( F5)下启动应用程序,而不是在没有调试的情况下 ( Ctrl+ F5),然后在 Visual Studio 的“输出”面板中选择“显示输出:调试”。