TheDeveloperBlog.com


ASP.NET IsDebuggingEnabled

IsDebuggingEnabled detects Debug mode on a website. Debug mode has severe performance negatives during runtime. We see an example of using the IsDebuggingEnabled property on HttpContext. We also look at the default setting in ASP.NET.


Example. First, we see how the IsDebuggingEnabled property is used in ASP.NET, in a simple Web Forms page. You can access the HttpContext anywhere with HttpContext.Current, not just in a Page.

Note: Here we have a Page code-behind file. The Response and Context objects are called intrinsic objects and are available in the Page.

Also: You can access these intrinsic objects by calling base.Response, for example.

Example that demonstrates IsDebuggingEnabled: C#

using System;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	Response.Write(Context.IsDebuggingEnabled);
	// also: // HttpContext.Current.IsDebuggingEnabled
    }
}


Default? Debug mode is not the default. I determined this is by creating a new web site project in Visual Studio, and then running the above page, Default.aspx. When you do not set debug="true" in Web.config, the site is compiled in Release mode.

Comment from Web.config

<!--
    Set compilation debug="true" to insert debugging
    symbols into the compiled page. Because this
    affects performance, set this value to true only
    during development.
-->

This comment hints that debug mode is only enabled when 'true' is set. However, it doesn't clearly say that unless true is set, the value is false. This article shows that false is the default and is used when the attribute is missing.


Summary. We saw the IsDebuggingMode property on HttpContext, which can be used when you are unsure if debugging is enabled. We also saw that you only need to worry about debugging mode at all if the attribute is present in Web.config.

Ideally: Your web application will have no behavioral difference in Debug mode. This would make it much easier to diagnose problems.