Determine whether or not ELMAH is enabled?
문제
How can I determine programmatically whether or not ELMAH is enabled?
해결책
Because:
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.
Of interest?: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute? (accepted answer is by ELMAH's author)
다른 팁
You can enumerate all loaded modules (via HttpApplication.Modules) and if Elmah module exists, then Elmah is enabled:
foreach (var m in Application.Modules) {
if (m is Elmah.ErrorlogModule) {
// ...
}
}
Not sure. Haven't treed this.
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.