Lor*_*ris 5 c# asp.net debugging
在任何(非web).net项目中,编译器会自动声明DEBUG和TRACE常量,因此我可以使用条件编译,例如,在调试与发布模式下以不同方式处理异常.
例如:
#if DEBUG
/* re-throw the exception... */
#else
/* write something in the event log... */
#endif
Run Code Online (Sandbox Code Playgroud)
如何在ASP.net项目中获得相同的行为?看起来web.config中的system.web/compilation部分可能是我需要的,但是如何以编程方式检查呢?或者我最好自己宣布DEBUG常量并在发布版本中对其进行评论?
编辑:我在VS 2008
要添加安德鲁斯答案的ontop,您也可以将其包装在一个方法中
public bool IsDebugMode
{
get
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
Run Code Online (Sandbox Code Playgroud)
看看ConfigurationManager.GetSection() - 这应该可以让你在那里大部分时间..但是,我认为你最好只是在调试和发布模式之间切换,让编译器决定执行"#if DEBUG"附带的语句.
#if DEBUG
/* re-throw the exception... */
#else
/* write something in the event log... */
#endif
Run Code Online (Sandbox Code Playgroud)
上面的工作会很好,只要确保你至少有两个构建配置(右键单击你正在处理的项目并转到"属性",那里有一个关于Builds的部分) - 确保其中一个构建检查了"定义DEBUG"而另一个没有.
这就是我最终做的事情:
protected bool IsDebugMode
{
get
{
System.Web.Configuration.CompilationSection tSection;
tSection = ConfigurationManager.GetSection("system.web/compilation") as System.Web.Configuration.CompilationSection;
if (null != tSection)
{
return tSection.Debug;
}
/* Default to release behavior */
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2992 次 |
| 最近记录: |