Today, I had to deliver a library of classes which allowed the registration of shares only in DEBUG.

When you deliver a NuGet package it needs to be optimized, you can't afford to deliver in DEBUG.

The problem is:

If I directly call the Debug.WriteLine method as follows:


Debug.WriteLine("debug");

The method works in DEBUG, but nothing is logged in RELEASE anymore.

If we take a look at theimplementation This attribute allows the compiler to tell not to compile the method if the DEBUG constant does not exist.

One possible solution:

Another way would be to use the Trace class and define the symbol TRACE” in class library...

But this way is not very clean, because we log in to listener Trace” instead of listener Debug”.

If this is used for something else, these logs cannot be disabled and will pollute the trace channel.

A more optimized solution:

After a little discussion with Thomas, he showed me how the .NET framework solves this problem: we define the DEBUG constant locally in the file where we want to log into DEBUG.


// A définir en début de fichier
#define DEBUG

Debug.WriteLine("debug");

and TADA (CamilleAll rights reserved ©), my software library can log in by debug without degrading its performance.

Indeed, if I had defined the symbol at the level of the entire class library, I would have activated behaviors available only in DEBUG.

Happy coding.