【发布时间】:2009-11-14 16:32:09
【问题描述】:
我有一个 ASP.NET Web 应用程序,并且我有一些我只想在调试版本中执行的代码。如何做到这一点?
【问题讨论】:
我有一个 ASP.NET Web 应用程序,并且我有一些我只想在调试版本中执行的代码。如何做到这一点?
【问题讨论】:
#if DEBUG
your code
#endif
您还可以将ConditionalAttribute 添加到仅在您以调试模式构建时才执行的方法:
[Conditional("DEBUG")]
void SomeMethod()
{
}
【讨论】:
Conditional 的方法的调用。
#if DEBUG 和 Conditional("DEBUG") 中的 DEBUG 被定义为 C# 编译器的命令行开关。
if (HttpContext.Current.IsDebuggingEnabled)
{
// this is executed only in the debug version
}
来自MSDN:
HttpContext.IsDebuggingEnabled 属性
获取一个值,指示当前 HTTP 请求是否处于调试模式。
【讨论】:
if (HttpContext.IsDebuggingEnabled)。
我在基本页面中声明了一个属性,或者您可以在应用程序中的任何静态类中声明它:
public static bool IsDebug
{
get
{
bool debug = false;
#if DEBUG
debug = true;
#endif
return debug;
}
}
然后实现你的愿望:
if (IsDebug)
{
//Your code
}
else
{
//not debug mode
}
【讨论】: