The namespace System.Diagnostics provides a set of attributes and classes to interact with the system process, event managers, performance counts etc. This namespace can help us too in debugging jobs.

Let’s review the useful actions inside System.Diagnostics namespace.

DebuggerDisplay Attribute

DebuggerDisplay attribute drives the string format with debug screen, which shows the value of: class, properties or fields.

For this same task, it is best known override ToString method, but use DebbugerDisplay attribute is a better choice, because this does not modify the data structure as it only interacts with Visual Studio debbuger screen. Override ToString method for only this purpose can give problems because many actions in .NET takes this value for default, for example bindings in WPF.

This attribute supports delegates, properties, fields and assamblies

Example 

  1. [System.Diagnostics.DebuggerDisplay("{ID} - {Model}- {Manufacturer} - {ProductionDate}")]  
  2. public class Car  
  3. {  
  4.     public int      ID             { get; set; }  
  5.     public string   Model        { get; set; }  
  6.     public string   Manufacturer   { get; set; }  
  7.     public DateTime ProductionDate { get; set; }  
  8. }   

.net

DebuggerHidden Attribute

DebuggerHidden attribute prevents the compiler from stopping in constructors, methods, properties and indexers declarations.

In mentioning this later, my comment might sound lightweight, but in the practice, this can save time push key F11 in debugging.

Example 

  1. [System.Diagnostics.DebuggerHidden]  
  2. public static List<Car> GetData()  
  3. {  
  4.     var result = new List<Car>()  
  5.     {  
  6.         new Car{ ID = 1, Manufacturer = "Ford",   Model = "Mustang", ProductionDate = DateTime.Today },  
  7.         new Car{ ID = 2, Manufacturer = "Nissan", Model = "Micra"  , ProductionDate = DateTime.Today }  
  8.     };  
  9.   
  10.     return result;  
  11. }   

Debugger.Launch

Occasionally, we can’t debug the code of a library, Service etc. because it is not accessible or we can’t add project to our solution. In this case, we can use the Debugger.Launch() method and Visual Studio opens a debug Window and we can debug its code.

.net

When executed, the line Systen.Diagnostics.Debbuger.Launch() opens a MessageBox with the instance of Visual Studio Debugger Options.

.net

In this window, we can choose, if we open a new stance of Visual Studio (all versions) or if we re-use an existing instance.

We can debug the code, as shown below.

.net

Conditional Attribute

Conditional attribute allows us to indicate a condition to the methods so that the compiler executes or does not execute its content.

We can use it with the precompiler sentences as DEBUG

  1. static void Main(string[] args)  
  2. {  
  3.   
  4.     DebugMethod();  
  5.   
  6.   
  7.     Console.Read();  
  8. }  
  9.   
  10. [System.Diagnostics.Conditional("DEBUG")]  
  11. public static void DebugMethod()  
  12. {  
  13.     Console.WriteLine("Execute Debug Method");  
  14. }   

It will only run if the solutions configurations are debugged.

The condition doesn’t exist for RELEASE, therefore we will use a define Directives.

Define Directives is another way to use System.Diagnostics.Conditional,

  1. #define RELEASE_MODE  
  2.   
  3. using System;  
  4. namespace SystemDiagnosticsUsefulActions  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ReleaseMethod();  
  11.   
  12.             Console.Read();  
  13.         }  
  14.   
  15.         [System.Diagnostics.Conditional("RELEASE_MODE")]  
  16.         public static void ReleaseMethod()  
  17.         {  
  18.             Console.WriteLine("Execute Release Method");  
  19.         }  
  20.   
  21.     }  
  22.   
  23. }   

These are the useful tips and traps of System.Diagnostics. They become very practical in many cases and I hope you find them useful.