.net Console.WriteLine() 与 Debug.WriteLine() 有什么区别?

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

What's the difference between Console.WriteLine() vs Debug.WriteLine()?

.netvb.net

提问by Zolomon

What's the difference between Console.WriteLine()vs Debug.WriteLine()?

什么之间的区别VS ?Console.WriteLine()Debug.WriteLine()

回答by Sam Holder

Console.WriteLinewrites to the standard output stream, either in debug or release. Debug.WriteLinewrites to the trace listeners in the Listenerscollection, but only when running in debug. When the application is compiled in the release configuration, the Debug elements will not be compiled into the code.

Console.WriteLine在调试或发布中写入标准输出流。Debug.WriteLine写入Listeners集合中的跟踪侦听,但仅在调试中运行时。在发布配置中编译应用程序时,不会将 Debug 元素编译到代码中。

As Debug.WriteLinewrites to all the trace listeners in the Listenerscollection, it is possible that this could be output in more than one place (Visual Studio output window, Console, Log file, third-party application which registers a listener (I believe DebugViewdoes this), etc.).

至于Debug.WriteLine写在所有跟踪监听器监听器集合,它是可能的,这可能是在多个地方输出(Visual Studio的输出窗口,控制台,日志文件,第三方应用程序,注册一个监听器(我相信DebugView中做到这一点), 等等。)。

回答by Hans Passant

Console.WriteLine()is meant for console mode programs. A nice feature of Visual Studio hosting process makes its output appear in the Visual Studio Output window while debugging for processes that don't have a console. That's very useful while debugging but beware that you should remove this code (or wrap it with a #ifdef DEBUG) when you're ready to create the Release build. It will otherwise add unnecessary overhead to your program. This makes it less than ideal for debug tracing.

Console.WriteLine()用于控制台模式程序。Visual Studio 托管进程的一个很好的功能是在调试没有控制台的进程时,它的输出显示在 Visual Studio 输出窗口中。这在调试时非常有用,但请注意,当您准备好创建发布版本时,您应该删除此代码(或使用 #ifdef DEBUG 包装它)。否则,它会给您的程序增加不必要的开销。这使得它不太适合调试跟踪。

Debug.WriteLine()generates tracing information if you build with the DEBUG conditional #defined. Which is on by default in the Debug build. Where the output ends up can be configured in the app.exe.config file. If this config is not overridden, .NET automatically provides an instance of the DefaultTraceListener class. It sends the Debug.WriteLine() text with the Windows OutputDebugString() API function to the debugger. The Visual Studio debugger makes that appear in the Output window, just like Console.WriteLine().

Debug.WriteLine()如果使用 DEBUG 条件 #defined 构建,则会生成跟踪信息。在 Debug 版本中默认开启。可以在 app.exe.config 文件中配置输出的最终位置。如果未覆盖此配置,.NET 会自动提供 DefaultTraceListener 类的实例。它将带有 Windows OutputDebugString() API 函数的 Debug.WriteLine() 文本发送到调试器。Visual Studio 调试器使它出现在输出窗口中,就像 Console.WriteLine() 一样。

A clear advantage of Debug.WriteLine() is that it generates no overhead in the Release build, the calls are effectively removed. It however does not support composite formatting, you'll need String.Format() for that. For debug tracing, the Debug class should be your choice.

Debug.WriteLine() 的一个明显优势是它在 Release 构建中不会产生开销,调用被有效地删除。但是它不支持复合格式,为此您需要 String.Format() 。对于调试跟踪,您应该选择 Debug 类。

回答by Hans Passant

If your purpose of using Console.WriteLine is solely for debugging, you better use Debug.WriteLine.

如果您使用 Console.WriteLine 的目的只是为了调试,则最好使用Debug.WriteLine

If you want to show a message to your user, you would use Console.WriteLine.

如果要向用户显示消息,可以使用Console.WriteLine

Debug.WriteLine is only for the purpose of debugging your application. In the release mode your debug statements will be ignored.

Debug.WriteLine 仅用于调试您的应用程序。在发布模式下,您的调试语句将被忽略。

Another usage of a console application is to test private assemblies. Rather than the traditional approach of creating some sort of GUI test harness to test the compiled version of the DLL, you can simply re-build the DLL as a console application and input/output from/to the console. I have found this technique to be faster than spending time creating a GUI test harness.

控制台应用程序的另一个用途是测试私有程序集。与创建某种 GUI 测试工具来测试 DLL 的编译版本的传统方法不同,您可以简单地将 DLL 重新构建为控制台应用程序并从/向控制台输入/输出。我发现这种技术比花时间创建 GUI 测试工具更快。