C# .NET BCL 中的跟踪与调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/179868/
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
Trace vs Debug in .NET BCL
提问by Ben Collins
It seems that the
看来,
are largely the same, with the notable exception that Debugusage is compiled out in a release configuration.
大体相同,但值得注意的例外是Debug用法是在发布配置中编译出来的。
When would you use one and not the other? The only answer to this I've dug up so far is just that you use the Debugclass to generate output that you only see in debug configuration, and Tracewill remain in a release configuration, but that doesn't really answer the question in my head.
你什么时候会使用一个而不是另一个?到目前为止,我挖出的唯一答案是您使用Debug类生成仅在调试配置中看到的输出,并且Trace将保留在发布配置中,但这并没有真正回答我的头。
If you're going to instrument your code, why would you ever use Debug, since Tracecan be turned off without a recompile?
如果您要检测代码,为什么要使用Debug,因为无需重新编译即可关闭Trace?
采纳答案by Jared
The main difference is the one you indicate: Debug is not included in release, while Trace is.
主要区别在于您指出的区别:发布中不包含调试,而跟踪包含。
The intended difference, as I understand it, is that development teams might use Debug to emit rich, descriptive messages that might prove too detailed (or revealing) for the consumer(s) of a product, while Trace is intended to emit the kinds of messages that are more specifically geared toward instrumenting an application.
据我了解,预期的区别在于,开发团队可能会使用 Debug 发出丰富的描述性消息,这些消息对于产品的消费者来说可能过于详细(或透露),而 Trace 旨在发出以下类型的消息:更专门用于检测应用程序的消息。
To answer your last question, I can't think of a reason to use Debug to instrument a piece of code I intended to release.
为了回答您的最后一个问题,我想不出使用 Debug 来检测我打算发布的一段代码的理由。
Hope this helps.
希望这可以帮助。
回答by Michael Burr
I'd look at using log4net for tracing as its capabilities are much more flexible and robust.
我会考虑使用 log4net 进行跟踪,因为它的功能更加灵活和强大。
But for true debug messages that I never intend for anyone other than me or an internal tester to see, I'd probably stick with Debug.
但是对于真正的调试消息,我从不打算让除我或内部测试人员以外的任何人看到,我可能会坚持使用 Debug。
回答by Cory Foy
You've answered your own question. If Debug messages stayed in, people could see them. For example, let's say you do:
你已经回答了你自己的问题。如果 Debug 消息保留,人们可以看到它们。例如,假设您执行以下操作:
Debug.WriteLine("Connecting to DB with username: blah and PW: pass");
Anyone who decompiles your code can see that. But that may be something vitally important for you to know during testing.
任何反编译您代码的人都可以看到这一点。但这可能对您在测试期间了解非常重要。
Trace is different. If you are going to do Trace, I'd likely just use log4net.
踪迹不同。如果您要进行跟踪,我可能只使用 log4net。
回答by Kevin Dente
For highly performance sensitive code blocks, leaving Trace compiled-in but disabled might make a performance difference.
对于高性能敏感代码块,保留 Trace 编译但禁用可能会产生性能差异。
回答by sandy101
The only difference between trace and debug is that trace statements are included by default in the program when it is compiled into a release build, whereas debug statement are not.
trace 和 debug 之间的唯一区别是,trace 语句在被编译到发布版本时默认包含在程序中,而 debug 语句则没有。
Thus, the debug class is principally used for debugging in the development phase, while trace can be used for testing and optimizationafter the application is compiled and released.
因此,debug 类主要用于开发阶段的调试,而trace 可以在应用程序编译发布后用于测试和优化。
回答by Raj
Debug is used to pure debugging purposes. It emits rich messages in debug execution (debug mode).
Debug 用于纯粹的调试目的。它在调试执行(调试模式)中发出丰富的消息。
Trace helps in application debugging, bug fixing, and profiling (after release).
跟踪有助于应用程序调试、错误修复和分析(发布后)。
The Debug class is of no use in release mode.
Debug 类在发布模式下没有用。
回答by Navid_pdp11
This is full difference between Trace and Debug: Both Debug and Trace use System.Diagnostics namespace.
这是 Trace 和 Debug 之间的完全区别:Debug 和 Trace 都使用 System.Diagnostics 命名空间。
Debug
调试
- It uses Debug class.
- It uses in debug build.
- It uses the time of application development.
- In Debug mode compiler inserts some debugging code inside the executable.
- Debug class works only in debug mode.
- Performance analysis cannot be done using Debug.
- Debugging uses to find error in program.
- For Debug we can use Debug.Write() method.
- Debug runs in same thread as main program execute.
- 它使用 Debug 类。
- 它用于调试构建。
- 它使用应用程序开发的时间。
- 在调试模式下,编译器会在可执行文件中插入一些调试代码。
- 调试类仅在调试模式下工作。
- 无法使用 Debug 进行性能分析。
- 调试用于发现程序中的错误。
- 对于调试,我们可以使用 Debug.Write() 方法。
- 调试在与主程序执行相同的线程中运行。
Trace
痕迹
- It uses Trace class.
- Trace statement includes by default when program compiled into released build.
- Trace class is used for testing and optimization even after an application is compiled and released.
- Trace class works in both case Debug mode as well as release mode.
- Trace runs in different thread form main program execute thread.
- For Trace we can use Trace.Write() method.
- It uses time of application deployment.
- 它使用 Trace 类。
- 当程序编译为发布版本时,默认情况下包含跟踪语句。
- 即使在编译和发布应用程序之后,跟踪类也用于测试和优化。
- 跟踪类在调试模式和发布模式下都适用。
- Trace 以不同的线程形式运行,主程序执行线程。
- 对于 Trace,我们可以使用 Trace.Write() 方法。
- 它使用应用程序部署时间。
reference : csharp corner
参考:csharp角