As I understand statements like Debug.WriteLine()
will not stay in the code in the Release build. On the other hand Trace.WriteLine()
will stay in the code in the Release build.
What is controling this behaviour? Does the C# compiler ignores everything from the System.Diagnostics.Debug
class when the DEBUG
is defined?
I am just trying to understand the internals of C# and just curious.
Trace works in both debug and logging mode, recording events as they occur in real-time. The main advantage of using trace over debugging is to do a performance analysis, which can't be accomplished on the debugging end. What's more, trace runs on a different thread. Thus, it doesn't impact the main code thread.
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.
TRACE designates finer grained informational events than the DEBUG. TRACE is level lower than DEBUG.
Print debugging or tracing is the act of watching (live or recorded) trace statements, or print statements, that indicate the flow of execution of a process and the data progression. Tracing can be done with specialized tools (like with GDB's trace) or by insertion of trace statements into the source code.
These methods use the ConditionalAttribute
to designate when they should be included.
When DEBUG
is specified as a #define
, via the command line or the system environment (set DEBUG = 1
in the shell), a method marked with [Conditional("DEBUG")]
will be included by the compiler. When DEBUG
is not included, those methods and any calls to them will be omitted. You can use this mechanism yourself to include methods under certain circumstances and it is also used to control the Trace
calls like Trace.WriteLine
(this uses the TRACE
define).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With