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.
-
1Another conversation about this problem http://stackoverflow.com/questions/179868/trace-vs-debug-in-net-bcl – Max Kilovatiy Jul 24 '14 at 06:04
2 Answers
The Debug
and Trace
classes have very similar methods. The primary difference is that calls to the Debug
class 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 ConditionalAttribute
annotating 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 ConditionalAttribute
in 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.

- 39,828
- 3
- 90
- 122
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.

- 6,816
- 15
- 55
- 79