我怎么能确定编程方式是否 ELMAH 是启用?

有帮助吗?

解决方案

因为:

ELMAH可以动态的加入 运行ASP.NET 网络应用程序,或者 甚至都ASP.NET 网上应用程序的一个 机,没有任何需要 重新编纂或重新部署。

你应该不需要探测是否存在。只是写你的记录代码,如果是,如果不是的话,什么都不会记录下来。

感兴趣?: 如何获得ELMAH的工作ASP.NET 视[HandleError]属性? (接受的答案是通过ELMAH是作者)

其他提示

可以枚举所有已加载的模块(通过的HttpApplication .Modules 的),并且如果ELMAH模块存在,则ELMAH启用:


foreach (var m in Application.Modules) {
  if (m is Elmah.ErrorlogModule) {
   // ...
  }
}

不确定。没有绿树此。

继塔达斯的回答,我想出了下面的代码为我工作(注意,我从VB没有检查,如果它编译翻译这一点,所以YMMV):

bool foundElmah = false;

foreach (var m in HttpContext.Current.ApplicationInstance.Modules) {
    var module = HttpContext.Current.ApplicationInstance.Modules.Item(m);
    if (module is Elmah.ErrorLogModule || module is Elmah.ErrorMailModule || module is Elmah.ErrorFilterModule || module is Elmah.ErrorTweetModule) {
        foundElmah = true;
        break;
    }
}

if (foundElmah) {
    // do something here, like populate the application cache so you don't have to run this code every time
    return true;
} else {
    // store in application cache, etc.
    return false;
}

这也得到周围我有请求elmah.axd(我使用Windows身份验证)时,得到一个401响应的问题,是要快得多,并且不承担elmah.axd特定的位置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top