System.Diagnostics.Debug.Write输出出现在哪里?

Fre*_*abe 138 c# debugging

以下C#程序(内置csc hello.cs)仅Hello via Console!在控制台和Hello via OutputDebugStringDebugView窗口中打印.但是,我看不到任何一个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" );
    }
}
Run Code Online (Sandbox Code Playgroud)

是否需要一些特殊的命令行开关csc

我没有使用Visual Studio进行任何开发,这是纯粹的命令行.

小智 110

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

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

  • 我想确认(Ctrl + Alt + O)将在Visual Studio 2012中调试时调出输出窗口 (4认同)

Tor*_*kår 73

正如其他人所指出的那样,必须注册听众才能阅读这些流.另请注意,Debug.Write只有在DEBUG设置了构建标志时Trace.Write才会起作用,而只有在设置了构建标志时才会起作用TRACE.

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

/define:DEBUG;TRACE

  • 我发现Debug和Trace对象使用相同的跟踪侦听器很有用(没想到),因此Debug.Write和Trace.Write输出到同一个地方,只是依赖于在他们可能无法执行的条件下 (4认同)
  • 这对我不起作用,我在vs项目属性中设置调试和跟踪标志,并且使用Debug.WriteLine,行执行,但输出窗口中没有任何内容,任何人有任何其他建议吗? (2认同)

jas*_*son 41

您需要添加一个TraceListener以查看它们出现在控制台上.

TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);
Run Code Online (Sandbox Code Playgroud)

在调试模式下,它们也出现在Visual Studio输出窗口中.

  • 显然DebugView将捕获.NET Debug.Write()和Win32 OutputDebugString():http://technet.microsoft.com/en-us/sysinternals/bb896647 (2认同)

Pat*_*Pat 10

在Visual Studio中进行调试时,显示"输出"窗口(视图 - >输出).它将显示在那里.


小智 5

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

你可以试试看。用于显示“立即”窗口(调试窗口立即)。