系统。诊断。调试。写输出出现在哪里?

下面的 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是否需要一些特殊的命令行开关?

我没有使用 VisualStudio 进行任何开发,这是纯命令行的东西。

215025 次浏览

在 VisualStudio 中进行调试时,显示“ Output”窗口(View-> Output)。

“输出”窗口中显示“诊断”消息。

您需要添加一个 TraceListener才能看到它们出现在控制台上。

TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);

当处于调试模式时,它们也会出现在 VisualStudio 输出窗口中。

正如其他人所指出的,侦听器必须注册才能读取这些流。还要注意,Debug.Write只有在设置了 DEBUG构建标志的情况下才能正常工作,而 Trace.Write只有在设置了 TRACE构建标志的情况下才能正常工作。

在 Visual Studio 的项目属性中,或者通过向 csc.exe 提供以下参数,可以很容易地设置 DEBUG和/或 TRACE标志

/define:DEBUG;TRACE

当调试 System.Diagnostics.Debug.WriteLine将显示在输出窗口(Ctrl + Alt + O)时,您还可以向 Debug.Listeners集合添加一个 TraceListener,以指定在其他位置输出的 Debug.WriteLine调用。

注意: 如果您在菜单 工具选择正在调试将军中选择了 Visual Studio 选项“将所有输出窗口文本重定向到即时窗口”,则 Debug.WriteLine调用可能不会显示在输出窗口中。要显示“ 工具选择正在调试”,请选中“ 工具选择工具0”旁边的框。

当我在代码中编写 Write (“”)时,它会在“即时窗口”中输出,而不是在“输出窗口”中输出。

你可以试一下。用于显示“即时”窗口(调试窗户马上)。

对于 VB.NET,以下内容适用。您必须选择“调试”,并确保您“开始调试”。这可以通过按 F5来达到。

此外,Console. WriteLine 只会在生成时在“ Output”窗口中显示消息。

如前所述,打开输出窗口与 观景输出和确保选择任一“建立”,如果你想看到控制台。如果要查看 Debug,请使用 WriteLine 消息或“ Debug”。写字或追踪。WriteLine 邮件。

我的解决办法是:

  1. 右键单击输出窗口;
  2. 检查“程序输出”