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?
+13
A:
#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()
{
}
empi
2009-11-14 16:34:36
how does it knows that it's in debug, is it taking it from web.config ?
Omu
2009-11-14 16:37:08
@Omu: I added the link to MSDN - it explains there how the compiler selectively removes calls to methods marked `Conditional`.
280Z28
2009-11-14 16:38:47
You choose the build mode between release or debug. #if and Conditional are used at compile time. Some more details: http://bytes.com/topic/c-sharp/answers/237540-conditional-debug-if-debug. You will find a lot information about in google.
empi
2009-11-14 16:39:33
@Omu: the `DEBUG` in `#if DEBUG` and `Conditional("DEBUG")` is defined as a command-line switch to the C# compiler.
dtb
2009-11-14 16:40:34
+1
A:
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
}
Shimmy
2009-11-14 16:35:24