74

I should probably know this already, but I'm not sure and I don't see it documented.

I use System.Diagnostics.Debug.WriteLine quite often during the development process to be able to track changes to variables or exceptions as I'm debugging the code. This is meant to make development and understanding what's happening easier only during development. I normally either comment out the code or delete it when I go to production.

I'm wondering what happens if I forget to comment the code out. Say, for example, that during the development cycle, I'm tracking error information that may log a connection sting to the output window using Debug.Write Line. This is obviously OK while developing, but I'm wondering if when I go live, if there is a risk here. Can someone attach a debugger to my live executable and trap this output? Or is it something that only produces output in Visual Studio?

And what about when we switch from debug to release? Does this code get ignored by the compiler if we compile for release?

Justin R.
  • 23,435
  • 23
  • 108
  • 157
David
  • 72,686
  • 18
  • 132
  • 173
  • 1
    Thank you all who answered. @Phil Devaney, @Mitch Wheat, and @Agent_9191 all gave pretty much the same answer in different wording, so I'm voting you all up, but I can only accept one answer. I'd go with who answered first, but that looks to be a tie betwee nMitch and Phil, so no offense, Mitch, but I'm going to give it to Phil, whose points are lower. – David Oct 21 '09 at 14:08
  • For others searching the following [Debug.WriteLine in release build](http://stackoverflow.com/questions/5419534/debug-writeline-in-release-build) may also prove interesting. – Joshua Drake Feb 16 '15 at 18:54

6 Answers6

94

All the members in the Debug class are marked with ConditionalAttribute, so the call sites won't be compiled into a Release build.

Phil Devaney
  • 17,607
  • 6
  • 41
  • 33
  • 28
    I wish I had a dollar for every time a co-worker sent out a production installer with the Debug build in it. – MusiGenesis Oct 21 '09 at 14:30
18

System.Diagnostics.Debug method calls are only present when the "DEBUG" conditional compilation symbol is defined. By default, the "DEBUG" symbol is defined only for debug builds.

Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol.

Steve
  • 213,761
  • 22
  • 232
  • 286
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
13

Since the Debug methods all have the [Conditional("DEBUG")] attribute on them, if you switch from Debug to Release you will have not have to worry about it as the calls to those methods will be removed (along with the other optimizations of a Release build).

Agent_9191
  • 7,216
  • 5
  • 33
  • 57
  • 3
    More specifically, don't define `DEBUG`. If you create a "Release" build (minimal symbols, optimized code, etc.) but define `DEBUG`, the `Debug` methods will still be called, right? – lmat - Reinstate Monica Jul 12 '12 at 23:15
7

Debug information is only visible when you're running in Debug mode. In Release mode no Debug statements will be visible (you can use Trace instead of Debug if you want these statements to be visible in Release mode).

http://support.microsoft.com/kb/815788

Marc
  • 1,798
  • 8
  • 15
2

Providing you compile without the /d:DEBUG option or #define DEBUG, your WriteLine calls will not physically present in your release code; there is no way for any third party to recover any information from these calls, as they literally not there in the release version.

More details here: Debug Class (System.Diagnostics) on MSDN

Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
1

Almost all members of Debug are marked with ConditionalAttribute. Such compilers as C# will skip calls to those methods during Release build, so you are on the safe side.

Mode info here: http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.aspx

Vitaliy Liptchinsky
  • 5,221
  • 2
  • 18
  • 25