Conditional Debugging

A program's capability to turn debugging on and off as needed is called conditional debugging. During development, output from debugging can clutter up normal output or force paths of execution that isn't necessary on every run. The System.Diagnostics namespace has both attributes and switches to turn debugging on and off as necessary. Listing 31.3 shows how to use attributes to control conditional debugging.

Listing 31.3. Debugging with Conditional Attributes: ConditionalDebugDemo.cs
 #define DEBUG using System; using System.Diagnostics; /// <summary> /// Conditional Debug Demo. /// </summary> class ConditionalDebugDemo { static bool Debugging = true; [Conditional("DEBUG")] static void SetupDebugListener() { TextWriterTraceListener ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.