Detecting ASP.NET Debug mode

Found this post earlier and well worth sharing;  I have wanted to know how to do this for sometime.  I have googled, searched books etc but never come up with the answer.

Fortunately there are smarter and more experienced people out there than me and Rick Strahl is one of them, if you don't subscribe to his blog I recommend it.

He has posted some code on how to find out if the application is running in debug mode...turns out its pretty simple and if I had spent sometime with intellisense (the best help source out there!) I would have found it.

Basically (ripped from Rick's Post).....

if (!HttpContext.Current.IsDebuggingEnabled)
    Script = OptimizeScript(Script);

Further.....there are a couple of comments than discuss the topic further.

From Wilco Bauwer he comments that this property encapsulates the web.config setting and doesn't take the page level debugging into account and it you wanted to....

bool isDebuggingEnabled = Assembly.GetExecutingAssembly().IsDefined(typeof(DebuggableAttribute));

....this is the kiddy to achieve it!!

and Peter Bromberg (C# MVP) offers up another solution using the Global.asax.cs file and a static global application flag being set in the Application_Start event.

public static bool IsDebugMode = false;
protected void Application_Start(object sender, EventArgs e)
{
   if (System.Diagnostics.Debugger.IsAttached) IsDebugMode = true;

This is definitely my find of the year (so far!)

Print | posted on Sunday, January 21, 2007 12:01 PM

Feedback

# re: Detecting ASP.NET Debug mode

Left by annalady at 9/23/2008 1:12 PM
Gravatar Hi,

Another way to do this, according to the configuration specified in the Web.Config file :

protected bool IsDebug
{
get
{
CompilationSection compilSec = (CompilationSection)ConfigurationManager.GetSection("system.web/compilation");

return compilSec.Debug;
}
}

# re: Detecting ASP.NET Debug mode

Left by chad at 5/14/2010 7:01 AM
Gravatar but how do you read the debug setting if you are running on a machine with machine.config's deployment->retail setting = true?

Your comment:





 

Copyright © Steve Clements

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski