4

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

THelper
  • 15,333
  • 6
  • 64
  • 104
lance
  • 16,092
  • 19
  • 77
  • 136
  • could you explain your question? as it stands it makes no sense (to me). – Mitch Wheat Jan 31 '10 at 05:30
  • Certainly. I'm shipping ELMAH with a product, but it's the customer's decision whether or not to enable or disable it (in the web.config). I need to know, when a page is loading, whether or not it's enabled. I'm happy to answer any questions. – lance Jan 31 '10 at 05:51
  • couldn't just read the relevant web.config section? – Mitch Wheat Jan 31 '10 at 05:53
  • I thought about that, but I was hoping, maybe, for something else, especially considering that the web.config section that tells you whether or not it's enabled changes from IIS6 to IIS7. For example, maybe something that gives me some sort of programmatic visibility to the handler for a given path... – lance Jan 31 '10 at 06:02

3 Answers3

2

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 Šukys
  • 4,140
  • 4
  • 27
  • 32
  • 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 Mar 05 '10 at 15:40
1

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)

Community
  • 1
  • 1
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • I need to show a link to elmah.axd. If it's disabled, I don't want to show the link? – lance Jan 31 '10 at 06:16
  • 1
    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 Feb 01 '10 at 17:42
  • @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 Feb 02 '10 at 14:36
0

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.

GregL
  • 37,147
  • 8
  • 62
  • 67