I ran into an issue where someone had apparently disabled the DEBUG
and TRACE
constants on a C# .NET project I was working on, so my calls to Debug.WriteLine
were having no effect. (No debug output was shown in the output.) After re-enabling them as described here, I started seeing my output.
Knowing how to fix it is helpful, but my question is why? As far as I understand, DEBUG
is a compile time constant, and the Debug
class is already compiled when I build my project. So how are my calls to Debug.WriteLine
skipped; shouldn't they be compiled like all my other code?
I can think of a few possible ways this could happen:
- MS implemented some special "feature" in the compiler to remove these calls without the constant
- Visual Studio sets up the debugger such that it does or doesn't listen based on this project setting for debug output when it runs
Debug
has some crazy code that examines the calling assembly for some kind of flag set at compile time
MS's documentation indicates that this is expected behavior, but I haven't been able to track down any documentation about how it actually works. It could also be something that never even occurred to me, of course.
So how does it work?