C# Debug 类和 Trace 类有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12984905/
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
What’s the difference between the Debug class and Trace class?
提问by Grasshopper
I am attempting to write better error-handling and debug logic in one of our applications. Can someone explain the difference between the Debug and Trace class? The documentation looks pretty similar. I want to use these classes in conjunction with NLog to improve our debugging efforts.
我正在尝试在我们的一个应用程序中编写更好的错误处理和调试逻辑。有人可以解释 Debug 和 Trace 类之间的区别吗?该文档看起来非常相似。我想将这些类与 NLog 结合使用来改进我们的调试工作。
回答by iefpw
Debug is used during debugging. Trace is writing to the log file. It is kind of like logging. Both are very similar, but do tracing for long term retention, debugging for real time debugging.
调试时使用调试。跟踪正在写入日志文件。这有点像日志记录。两者非常相似,但进行长期保留的跟踪,实时调试的调试。
回答by Mike Zboray
The Debugand Traceclasses have very similar methods. The primary difference is that calls to the Debugclass are typically only included in Debug build and Trace are included in all builds (Debug and Release). You can control this through the compiler flags DEBUG and TRACE. If you look at the documentation for both, you will notice the ConditionalAttributeannotating the methods. This causes the method calls to be included in the binaries only when the appropriate compiler flag is defined. You could define your own compiler flag and use it in conjunction with the ConditionalAttributein a similar fashion. Note that if you use this, the methods are not removed from the compiled binaries. The call sites are modified to remove the method calls.
在Debug和Trace班有非常相似的方法。主要区别在于对Debug类的调用通常仅包含在 Debug 构建中,而 Trace 包含在所有构建中(调试和发布)。您可以通过编译器标志 DEBUG 和 TRACE 来控制这一点。如果您查看两者的文档,您会注意到ConditionalAttribute对方法的注释。这会导致只有在定义了适当的编译器标志时才将方法调用包含在二进制文件中。您可以定义自己的编译器标志,并ConditionalAttribute以类似的方式将其与 结合使用。请注意,如果您使用它,则不会从编译的二进制文件中删除这些方法。修改调用站点以删除方法调用。

