Contact   •   Articles   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

Detecting ASP.NET Debug mode

Ah the beauty of a big framework. I was doing some work on an internal GZip compression handler and one thing I needed to know is whether the application is running in Debug mode. It’s easy to do this IF you know where to look:


// *** Optimize the script by removing comment lines and stripping spaces

if (!HttpContext.Current.IsDebuggingEnabled)           

    Script = OptimizeScript(Script);   


It took me a while to find this - I was expecting to find this on the HttpApplication object. Didn't have much luck with searching either when finally I stumbled onto it above on the Context object.


Although this is probably not a setting you deal with on a daily basis it can certainly be useful when sending certain output back to the client and of course for test and trace scenarios in your own application code. In this case, the idea is to send back the original file JavaScript.js file and only optimize and mangle that text when not running in debug mode.



Make Donation
Posted in ASP.NET  

Feedback for this Post

# re: Detecting ASP.NET Debug mode
by Wilco Bauwer January 20, 2007 @ 6:05am
If I remember correctly, this property basically encapsulates the web.config setting. It doesn't actually take the page-level debug directive into account. If you want to take this into account, you should actually see if the DebuggableAttribute is applied on the current assembly. That goes something like this:

bool isDebuggingEnabled = Assembly.GetExecutingAssembly().IsDefined(typeof(DebuggableAttribute));
# re: Detecting ASP.NET Debug mode
by Peter Bromberg January 20, 2007 @ 2:27pm
Interesting. I've always done this (for what it's worth):

// Global.asax.cs
public static bool IsDebugMode = false;
protected void Application_Start(object sender, EventArgs e)
if (System.Diagnostics.Debugger.IsAttached) IsDebugMode = true;
# re: Detecting ASP.NET Debug mode
by Rick Strahl January 20, 2007 @ 2:45pm
Wilco - thanks for the clarification. That does complicate things in true debug scenarios. In my case though this won't be an issue as I need to decide what output to send back - the config setting suffices for that.

Peter - interesting! IsDebugger.IsAttached addresses a whole other. In this case I'm not after knowing whether the user is actually running a debugger or not but rather whether the application is in Debug Mode. Like MS Ajax I'm trying to send back Client script code and depending on the debug flag I either want to mangle the script or not and add the appropriate browser caching flags (public vs. private) so that the content can refresh.

Hey, even a simple thing like this has some complex ramifications in ASP.NET <s>...
# re: Detecting ASP.NET Debug mode
by Steve January 21, 2007 @ 5:01am
Excellent post - I have been looking for this on and off for ages!

Tip of the year (so far!)

# re: Detecting ASP.NET Debug mode
by Kaerber January 21, 2007 @ 6:55am
What about
Script = OptimizeScript( Script );

# re: Detecting ASP.NET Debug mode
by Stefan Holmberg January 21, 2007 @ 11:10am
Interesting - but I use my own solution. I have my "own" web.config appsettings debugmode flag which I turn on and off for stuff like script optimization. My point: app being compiled in debug or release mode, page level directives being set or not etc shouldn't matter. Sometimes it's easiest to just modify a flag in a config file (even directly on the production server where app is compiled in release mode etc with no debugging options) and still get a nonoptimized script out, i.e something overriding all other options.
# re: Detecting ASP.NET Debug mode
by Rick Strahl January 21, 2007 @ 1:45pm
Kaerber, your solution works only in certain fixed compilation scenarios. Specifically I think only in the actual Web project and then only if the app isn't precompiled. Otherwise the value won't be dynamic.

For my scenarion here I need to know at runtime and from within a custom control whether whether debug mode is enabled in the Web app. The control almost itself certainly won't be in debug mode <s>.

Stefan - I agree for application level settings. I do the same I use a custom configuration class ( that stores in web.config as well for those things. But if I'm working on custom controls one of the things I want to avoid is having users have to make any changes anywhere if possible other than what they normally do - ie. low impact installation. Otherwise you end up with a mess like MS Ajax in web.config <s>...
# re: Detecting ASP.NET Debug mode
by Pieter Siegers January 23, 2007 @ 11:47am
Hi Rick, like Kaerber, in Anthem.NET we're using the #if DEBUG statement, which is sufficient. Because only when developers want to change some code they will run VS.NET in Debug Mode using the latest CVS code. Anyways, thanx for the interesting post (and the interesting replies it generated)!
# re: Detecting ASP.NET Debug mode
by Sean February 23, 2007 @ 5:43am
There is another option, which I like a lot because it's more dynamic:
This way you don't have to be assembly-compilation oriented, but truly dynamic. If you attach a debugger to your process, then your script will not get compacted and you would be able to fully debug
# Peter Bromberg's UnBlog: Is Debug Mode Evil?
by Peter Bromberg's UnBlog February 28, 2007 @ 6:02am
# re: Detecting ASP.NET Debug mode
by Christoff May 25, 2007 @ 3:12am
Does anyone have a simple way of attaching an event that gets fired once the debugger isnt attached anymore? Application_end wont be fired since that only gets fired on server shutdown etc, not on debugger exit? Any ideas? Other than attaching yourself to the current running process?
# DotNetSlackers: Detecting ASP.NET Debug mode
by DotNetSlackers Latest ASP.NET News June 01, 2007 @ 9:00am
# re: Detecting ASP.NET Debug mode
by Chris December 12, 2007 @ 10:29am
I used to use a boolean flag in my code to determine if it's running in debug, but then I just went to the source:

Private DEBUG_ENABLED As String = ConfigurationSettings.GetConfig("system.web/compilation").Debug.ToString

If DEBUG_ENABLED Then something...
# re: Detecting ASP.NET Debug mode
by Rick Strahl December 12, 2007 @ 12:22pm
That should work but there's no need to do this. The IsDebuggingEnabled will give you that same setting.
# re: Detecting ASP.NET Debug mode
by Nathan February 14, 2008 @ 5:04pm
Thanks Rick, exactly what I was looking for!
# re: Detecting ASP.NET Debug mode
by Michael Schall February 20, 2008 @ 9:15am
I think I found an issue with IsDebuggingEnabled...

It does not seem to honor the retail flag in the machine.config described here

Any thoughts on a work around?

# re: Detecting ASP.NET Debug mode
by ivan February 18, 2009 @ 12:21am
Hi, all!
we are experiencing a problem with debug mode in one application in our test and production environments. We have disabled debug mode through web.config with <compilation debug="false">. In the code there is one point when the developer put this section:


We have detected that at least 3 times the DoSomething() function code is executed and this could not be right. Can be debug mode altered by something during execution? Maybe something in the IIS? Or machine configuration? It seems like it has no sense, but I can´t find the solution.

Thanks a lot for your help
# re: Detecting ASP.NET Debug mode
by Rick Strahl February 18, 2009 @ 5:24am
@Ivan - The debug setting in web.config is different from the DEBUG pre-compiler constant, so no you can't change #if DEBUG base code at runtime. You need to recomile.

You can use the above to replace that #if DEBUG code however to effect the same thing. You can always check for HttpContext.Current.IsDebuggingEnabled to figure out whether a Web app is runnign with debugmode="true"
# re: Detecting ASP.NET Debug mode
by Ryo May 07, 2009 @ 9:18pm
I have the same problem with Ivan. Can u explain in a bit more detail about how to overcome the DEUG pre-compiler problem?

Cos my debug has always been false in my <compilation debug="false">

Even when I recompile, it still runs the code within the #IF DEBUG section.

I prefer to use #IF DEBUG so that the code won't be run in production mode. (As opposed to having the HttpContext.Current.IsDebuggingEnabled check all the time).


# re: Detecting ASP.NET Debug mode
by Rick Strahl May 08, 2009 @ 1:02pm
@Ryo - please re-read my previous comment. <compilation debug="true"> is not the same as the DEBUG symbol that determines debug mode during compilation. These are completely different settings and apply to different things.

#DEBUG switch maps to Debug mode on your Web project or more precisely the DEBUG symbol that the debug compilation mode sets.
# re: Detecting ASP.NET Debug mode
by Justin May 19, 2009 @ 11:25pm
This was an interesting problem for me.
Nothing seamed to work.
I needed to detect debug so i could emit html to a startup page because my dynamic HTML app used debug code signifcantly different than release and i did not want to go down the path of haveing a indexDebug.aspx file.
I tried #if DEBUG
But to my amazment it was always true even after compileing my website in Release configuration.

in the end i added a small C# static class library as a reference to my "Web Project" and just called it from the Page_Load function attached to the aspx file.
namespace ConfigurationDetector
    static public class Config
        static public bool IsDebug
            get{ return true;}
            get { return false; }

Then i just call it from my Page_Load
   protected void Page_Load(object sender, EventArgs e)
        if (Config.IsDebug)


# re: Detecting ASP.NET Debug mode
by Rick Strahl May 20, 2009 @ 10:59am
I really wonder how many people actually READ the posts completely before posting a comment. It's amazing to see how many people reply here with the same question confusing DebugMode in web.config with Debug and Release modes in the compiler.
# re: Detecting ASP.NET Debug mode
by mike May 21, 2009 @ 1:24pm
Hello Rick,
Thanks for the interesting points. Although a bit of a newbie, I also believe there is a difference between compiling with /debug and /realease and the DEBUG constant used in pre-compiler processing. Anyhow, I'm going on a tangent here... but I've been trying to understand if local path information gets stored in assembly information when you compile with release option. I've had no luck finding the answer online. I'm trying to understand if it's important to have the same directory structure on our build server as on our developer machines. I'm thinking no, but I'm being told otherwise (I guess I just don't believe it). It's my understanding that some of this information may be included in debug files to help debugging, but gets excluded when optimizing a release.

Thanks for your help and your interesting posts.
# re: Detecting ASP.NET Debug mode
by Sameer June 05, 2009 @ 8:55am
This is so funny. I think like 5 different people keep asking the same question.......... guys just read the comments carefully before asking again about #if DEBUG
# re: Detecting ASP.NET Debug mode
by Nick T July 29, 2009 @ 8:27am
Great like a charm.

Funniest comments ever.
# re: Detecting ASP.NET Debug mode
by Sean February 04, 2010 @ 12:46pm
Great find! Really useful post for me.

Comments deserve a facepalm don't they?
# re: Detecting ASP.NET Debug mode
by NetWave March 05, 2010 @ 1:05am

Let me first say I read ALL comments.

You said: "<compilation debug="true"> is not the same as the DEBUG symbol that determines debug mode during compilation. These are completely different settings and apply to different things."

What you say is certainly true for Web Application Projects. You either build it in DEBUG or RELEASE mode, hence the DEBUG symbol is set or not.

But what about Web Site Projects where compilation is postponed until runtime? Won't ASP.NET compiler set the DEBUG symbol when your web.config contains <compilation debug="true"> ?
# re: Detecting ASP.NET Debug mode
by Tim G January 25, 2011 @ 12:52pm
NetWave, I don't understand your comment. In ASP.NET web apps you have both pre-compiled binaries (e.g. your class libraries and code behind files) and dynamically compiled aspx pages. I haven't really used the #IF DEBUG directive, but I would assume that its value in your binaries must be whatever it was when they were compiled (since it's a compiler directive). Changing your web.config (including changing debug=false to debug=true) won't cause those files to be recompiled. (Probably you won't see this all these months later but I had to ask.)
# re: Detecting ASP.NET Debug mode
by fc July 26, 2011 @ 3:50pm
Thanks Rick! Believe it or not, 4.5 years later your blog is still saving lives (at least mine). Thanks a bunch!
# re: Detecting ASP.NET Debug mode
by Ryan Helms August 23, 2011 @ 2:02pm

As usual, you are the end to my question. Thanks for all you do.


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2015