Geeks With Blogs

Steve Clements .Net and then some....
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!)

Posted on Sunday, January 21, 2007 12:01 PM | Back to top

Comments on this post: Detecting ASP.NET Debug mode

# re: Detecting ASP.NET Debug mode
Requesting Gravatar...

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

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

return compilSec.Debug;
Left by annalady on Sep 23, 2008 1:12 PM

# re: Detecting ASP.NET Debug mode
Requesting Gravatar...
but how do you read the debug setting if you are running on a machine with machine.config's deployment->retail setting = true?
Left by chad on May 14, 2010 7:01 AM

Your comment:
 (will show your gravatar)

Copyright © Steve Clements | Powered by: