Search code examples
c#.netdebugginglistenertrace

Debug.Listeners and Trace.Listeners are equal by reference


By experimenting a little with Debug and Trace classes, I came upon the fact that Debug.Listeners is referencially equivalent to Trace.Listeners.

So these happen

Console.WriteLine(Debug.Listeners == Trace.Listeners); // true
Console.WriteLine(ReferenceEquals(Debug.Listeners, Trace.Listeners)); // true

This makes impossible to log Debug and Trace messages differently.

Is this a bug, or a feature; and more importantly: why does it work like that?


Solution

  • Yes, it is feature (in sense it is documented - Trace.Listeners). You have list of listeners that send logged messages to some destination (trace window, console, file,...).

    You can separate control whether Debug.xxx and Trace.xxx statement do anything by defining/un-defining corresponding symbols. Normally your "Debug" build defines both DEBUG and TRACE while "Release" configuration only defines TRACE conditional compilation symbol. You can see that in project's properties on "Build" tab.