Determine if ELMAH is enabled?

How can I determine programmatically if ELMAH is enabled?

+4
source share
3 answers

Because:

ELMAH can be dynamically added to run ASP.NET web applications or even all ASP.NET web applications on machines, without any re-compilation or redeployment.

you do not need to determine if he is present. Just write your registration code as if it were present, and if not, nothing will be registered.

Interesting: How to get ELMAH to work with the ASP.NET MVC [HandleError] attribute? (accepted answer by ELMAH)

+1
source

You can list all loaded modules (via HttpApplication.Modules ), and if the Elmah module exists, then Elmah is enabled:

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

Not sure. Did not look at it.

+2
source

In response to Tadas answer, I came up with the following code that works for me (note that I translated this from VB without checking if it compiles, therefore 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 addresses the issues that I encountered getting a 401 response when requesting elmah.axd (I used Windows authentication) and is much faster, and I don't assume this is the place for elmah.axd.

0
source

Source: https://habr.com/ru/post/1299863/


All Articles