9

I'm using Elmah for logging exceptions on my MVC application using Alex Beletsky's elmah-mvc NuGet package.

The application registers some global filters, applied on each action called.

Is there a way to prevent some of those filters from being applied when calling the Elmah.Mvc.ElmahController error log page (foo.com/elmah) ?

A test like below works, of course, but I'm looking for a more elegant way that would not involve modifying the filter (nor the source code from Elmah / Elmah MVC). Is it even possible ?

public class FooAttribute : FilterAttribute, IActionFilter
{
    // ...

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.Controller is ElmahController)
        {
            return;
        }

        // do stuff
    }
}
  • I know that attributes can't be added or removed at runtime.

  • I thought of wrapping the ElmahController in a new one where I could add an exclusion filter, but I'm not sure how (if possible) to change the web.config to reference this wrapper instead of the original controller.

Community
  • 1
  • 1
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
  • Check this - http://stackoverflow.com/questions/9953760/how-to-disable-a-global-filter-in-asp-net-mvc-selectively – malkam Jan 28 '15 at 13:12
  • @malkam I know this one. The problem is that it involves decorating the action with an attribute, i.e. modifying Elmah source code, which I don't want to do, for obvious reasons. – xlecoustillier Jan 28 '15 at 13:18
  • this solution doesn't seem so terrible to me. I would probably do it in the positive though: `if(controller is ElmahController) { //return with no special actions` – DLeh Jan 28 '15 at 17:03
  • @DLeh You're right, it's not so bad, I agree. I'm just looking to ways of achieving this without having to alter the filter itself, so that I don't have to alter each and every filter with ElmahController when I'll need to exclude them as well. – xlecoustillier Jan 28 '15 at 19:31
  • maybe you could make your filters extend a base filter that has this code in one spot? – DLeh Jan 28 '15 at 19:33
  • @DLeh That's the same in the end. As soon as I'll need to exclude a filter, I'll have to make it extend this base class, i.e. modify it. Not mentioning filters already extending another class. – xlecoustillier Jan 29 '15 at 07:52

1 Answers1

6

You could register your global filters through a custom IFilterProvider:

public class MyFilterProvider : IFilterProvider
{
    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        if (controllerContext.Controller is ElmahController)
        {
            return Enumerable.Empty<Filter>();
        }

        return ... the collection of your global filters
    }
}

and in your Application_Start instead of calling:

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

you would call:

FilterProviders.Providers.Add(new MyFilterProvider());
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • That's the idea. I'll have to get the internal Elmah.Mvc.HandleErrorAttribute from the global filters (it gets automatically registered during app start http://beletsky.net/2012/06/elmahmvc-v200-release-candidate.html), but I guess this is the solution I'm looking for. Thanks! – xlecoustillier Jan 29 '15 at 10:33