ASP.NET IsDebuggingEnabled

ASP.NET web programming framework

You want to detect whether your ASP.NET website is running in Debug mode, or Release mode. 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.

This C# example shows how to use IsDebuggingEnabled 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.

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
    }
}

Note on intrinsic objects. Here we have a Page code-behind file. The Response and Context objects are called intrinsic objects and are available in the Page. You can also use them by calling base.Response, for example.

Is Debug mode the default?

Question and answer

No—the way 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. Here is the default comment.

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

Repeating the result. 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

The C# programming language

We saw the IsDebuggingMode property on HttpContext, which can be used when you are unsure if debugging is enabled. Additionally, we 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 in Debug mode that happen in Release mode.

ASP.NET Tutorials
.NET