如果我将System.Diagnostics.Debug.WriteLine放在Global.asax.cs中,输出会去哪里?

12 浏览
0 Comments

如果我将System.Diagnostics.Debug.WriteLine放在Global.asax.cs中,输出会去哪里?

以下的C#程序(使用csc hello.cs编译)仅在控制台上打印Hello via Console!,并在DebugView窗口中打印Hello via OutputDebugString。然而,我无法看到System.Diagnostics.*调用。为什么会这样呢?

using System;
using System.Runtime.InteropServices;
class Hello {
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    public static extern void OutputDebugString(string message);
    static void Main() {
        Console.Write( "Hello via Console!" );
        System.Diagnostics.Debug.Write( "Hello via Debug!" );
        System.Diagnostics.Trace.Write( "Hello via Trace!" );
        OutputDebugString( "Hello via OutputDebugString" );
    }
}

也许在csc中需要一些特殊的命令行开关吗?

我没有使用Visual Studio进行任何开发,这是纯命令行操作。

admin 更改状态以发布 2023年5月24日
0
0 Comments

就像其他人指出的那样,必须注册侦听器才能读取这些流。同时注意到只有在设置了DEBUG生成标志时Debug.Write才能正常运行,而只有在设置了TRACE生成标志时Trace.Write才能正常运行。

在Visual Studio的项目属性中或通过向csc.exe提供以下参数即可轻松设置DEBUG和/或TRACE标志

/define:DEBUG;TRACE

0
0 Comments

在调试时,System.Diagnostics.Debug.WriteLine会在输出窗口(Ctrl+Alt+O)中显示,但你也可以将TraceListener添加到Debug.Listeners集合中,以指定Debug.WriteLine调用在其他位置输出。

注意:如果你在Visual Studio选项中的菜单“工具→选项→调试→常规”下勾选了“将所有输出窗口文本重定向到即时窗口”,那么Debug.WriteLine调用可能不会显示在输出窗口中。要显示“工具→选项→调试”,请在“工具→选项→显示所有设置”旁边勾选框。

0