ELMAH can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
you should not need to detect whether it is present. Just write your logging code as if it was present, and if it's not, nothing will be logged.
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;
}
这也解决了我在获取 401 响应时遇到的问题请求 elmah.axd(我使用的是 Windows 身份验证),速度更快,并且不假定 elmah.axd 的特定位置。
Further to Tadas' answer, I came up with the following code that works for me (note that I have translated this from VB without checking if it compiles, so 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;
}
This also gets around the problems I had with getting a 401 response when requesting elmah.axd (I was using Windows Authentication), and is much faster, and doesn't assume a specific location for elmah.axd.
发布评论
评论(3)
您可以枚举所有加载的模块(通过 HttpApplication.Modules< /a>),如果 Elmah 模块存在,则 Elmah 已启用:
不确定。这个还没有树过
You can enumerate all loaded modules (via HttpApplication.Modules) and if Elmah module exists, then Elmah is enabled:
Not sure. Haven't treed this.
因为:
您不需要检测它是否存在。只需编写您的日志记录代码,就好像它存在一样,如果不存在,则不会记录任何内容。
感兴趣吗?: 如何让 ELMAH 与 ASP.NET MVC [HandleError] 属性一起使用?(接受的答案是 ELMAH 的作者)
Because:
you should not need to detect whether it is present. Just write your logging code as if it was present, and if it's not, nothing will be logged.
Of interest?: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute? (accepted answer is by ELMAH's author)
除了 Tadas 的回答之外,我想出了以下适合我的代码(请注意,我已从 VB 翻译了此代码,但没有检查它是否编译,所以 YMMV):
这也解决了我在获取 401 响应时遇到的问题请求 elmah.axd(我使用的是 Windows 身份验证),速度更快,并且不假定 elmah.axd 的特定位置。
Further to Tadas' answer, I came up with the following code that works for me (note that I have translated this from VB without checking if it compiles, so YMMV):
This also gets around the problems I had with getting a 401 response when requesting elmah.axd (I was using Windows Authentication), and is much faster, and doesn't assume a specific location for elmah.axd.