Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug vs Trace in C#

Tags:

c#

debugging

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.

like image 842
koumides Avatar asked Feb 17 '11 15:02

koumides


People also ask

What is difference between debug and trace?

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.

What's the difference between the debug class and trace class?

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.

Is trace higher than debug?

TRACE designates finer grained informational events than the DEBUG. TRACE is level lower than DEBUG.

What is live debugging and tracing?

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.


1 Answers

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).

like image 165
Jeff Yates Avatar answered Oct 06 '22 01:10

Jeff Yates