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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1159755/
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-06 09:38:19  来源:igfitidea点击:

Where does System.Diagnostics.Debug.Write output appear?

c#debugging

提问by Frerich Raabe

The following C# program (built with csc hello.cs) prints just Hello via Console!on the console and Hello via OutputDebugStringin the DebugView window. However, I cannot see either of the System.Diagnostics.*calls. Why is that?

以下 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" );
    }
}

Is there maybe some special command-line switches required for csc?

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

I'm not using Visual Studio for any of my development, this is pure commandline stuff.

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

采纳答案by Tormod Fjeldsk?r

As others have pointed out, listeners have to be registered in order to read these streams. Also note that Debug.Writewill only function if the DEBUGbuild flag is set, while Trace.Writewill only function if the TRACEbuild flag is set.

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

Setting the DEBUGand/or TRACEflags is easily done in the project properties in Visual Studio or by supplying the following arguments to csc.exe

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

/define:DEBUG;TRACE

/define:DEBUG;TRACE

回答by Pat

While you are debugging in Visual Studio, display the "Output" window (View->Output). It will show there.

在 Visual Studio 中调试时,显示“输出”窗口(视图->输出)。它会显示在那里。

回答by Andreas Grech

The Diagnostics messages are displayed in the Output Window.

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

回答by jason

You need to add a TraceListenerto see them appear on the Console.

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

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

They also appear in the Visual Studio Output window when in Debug mode.

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

回答by boardernin

While debugging System.Diagnostics.Debug.WriteLinewill display in the output window (Ctrl+Alt+O), you can also add a TraceListenerto the Debug.Listenerscollection to specify Debug.WriteLinecalls to output in other locations.

在调试System.Diagnostics.Debug.WriteLine将在输出窗口(显示Ctrl+ Alt+ O),你还可以添加TraceListenerDebug.Listeners收藏指定Debug.WriteLine在其他地点调用输出。

Note: Debug.WriteLinecalls may not display in the output window if you have the Visual Studio option "Redirect all Output Window text to the Immediate Window" checked under the menu ToolsOptionsDebuggingGeneral. To display "ToolsOptionsDebugging", check the box next to "ToolsOptionsShow All Settings".

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

回答by kykbr

When I write debug.write("")in the code, it outputs in the "Immediate window", not "Output window".

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

You can try it. For displaying the "Immediate" window (DebugWindowImmediate).

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

回答by Matthis Kohli

For VB.NET the following applies. You have to select "Debug" AND make sure that you "Start Debugging". This can be reached by pressing F5.

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

Also the Console.WriteLine will only display messages when building as "Release" in your Output window.

此外,Console.WriteLine 只会在输出窗口中构建为“发布”时显示消息。

As mentioned before, open the Output window with ViewOutputAND make sure to select either "Build" if you want to see Console.WriteLine messages or "Debug" if you want to see Debug.WriteLine or Trace.WriteLine messages.

如前所述,使用ViewOutput打开输出窗口,并确保选择“Build”(如果您想查看 Console.WriteLine 消息)或“Debug”(如果您想查看 Debug.WriteLine 或 Trace.WriteLine 消息)。

回答by Ning Zhu

The solution for my case is:

我的情况的解决方案是:

  1. Right click the output window;
  2. Check the 'Program Output'
  1. 右键单击输出窗口;
  2. 检查“程序输出”