Omu
Omu

Reputation: 71218

How to execute code only in debug mode in ASP.NET

I have an ASP.NET web application and I have some code that I want to execute only in the debug version. How to do this?

Upvotes: 36

Views: 36086

Answers (3)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

I declared a property in my base page, or you can declare it in any static class you have in applicaition:

    public static bool IsDebug
    {
        get
        {
            bool debug = false;
#if DEBUG
            debug = true;
#endif
            return debug;
        }
    }

Then to achieve your desire do:

    if (IsDebug)
    {
        //Your code
    }
    else 
    {
        //not debug mode
    }

Upvotes: 11

dtb
dtb

Reputation: 217313

Detecting ASP.NET Debug mode

if (HttpContext.Current.IsDebuggingEnabled)
{
    // this is executed only in the debug version
}

From MSDN:

HttpContext.IsDebuggingEnabled Property

Gets a value indicating whether the current HTTP request is in debug mode.

Upvotes: 64

empi
empi

Reputation: 15881

#if DEBUG
your code
#endif

You could also add ConditionalAttribute to method that is to be executed only when you build it in debug mode:

[Conditional("DEBUG")]
void SomeMethod()
{
}

Upvotes: 76

Related Questions