views:

106

answers:

2

How can I determine programmatically whether or not ELMAH is enabled?

+1  A: 

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)

Mitch Wheat
I need to show a link to elmah.axd. If it's disabled, I don't want to show the link?
lance
Can you make a web request to elmah.axd and check the status code? Maybe do this once and store the results in the Application variable for future uses.
Greg
@Greg: I was hoping to avoid that overhead. Your solution is what I've coded so far, as it's the best I could think of. I'm going to try looking through the loaded modules list, per Tadas' answer above.
lance
+1  A: 

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.

Tadas
I tried this, but Application.Modules wasn't available (it's been a while since I tried -- I might be remembering the details incorrectly, but I couldn't make it happen). Do you have actual code that you've used to do this?
lance