Making sure your application is not left in debug="true"

You will find loads of articles around the web reminding us all that running your production ASP.NET website in complication debug=true mode is a BAD THING!

To make sure we avoid this problem we have added a start up check in our application that will make sure that the build mode (debug or release) of our binaries match that of the web.config. As all releases are done via our build server all our binaries will be in release mode. However we tend to make major changes to the web.config on the live servers so deploying the web.config is usually not an option.

To check if the web.config has debug enabled you can use HttpContext.Current.IsDebuggingEnabled.

To check what mode a binary is in, we use the #DEBUG Preprocessor Directive:

<span style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">// codesnippet:ADB0358E-3BD1-11DE-B2D2-764056D89593</span>
<span style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> BuildModeEnum BuildMode
<span style="color: rgb(96, 96, 96);">   3:</span> {    
<span style="color: rgb(96, 96, 96);">   4:</span>     get    
<span style="color: rgb(96, 96, 96);">   5:</span>     {        
<span style="color: rgb(96, 96, 96);">   6:</span>         <span style="color: rgb(204, 102, 51);">#if</span> (DEBUG)
<span style="color: rgb(96, 96, 96);">   7:</span>             <span style="color: rgb(0, 0, 255);">return</span> BuildModeEnum.Debug;
<span style="color: rgb(96, 96, 96);">   8:</span>         <span style="color: rgb(204, 102, 51);">#else</span>
<span style="color: rgb(96, 96, 96);">   9:</span>             <span style="color: rgb(0, 0, 255);">return</span> BuildModeEnum.Release;
<span style="color: rgb(96, 96, 96);">  10:</span>         <span style="color: rgb(204, 102, 51);">#endif</span>
<span style="color: rgb(96, 96, 96);">  11:</span>     }
<span style="color: rgb(96, 96, 96);">  12:</span> }