سؤال

I am using a global ErrorHandler filter to log unhandled exceptions, but I would also like to be able to use this same handler to log handled exceptions from within a catch block.

I would like to access the global filters and execute the ErrorHandler as configured, but I'm having some difficulty doing this. I tried the following, but Resharper is telling me that the filter can't ever be my ErrorLogFilter.

foreach (var filter in GlobalFilters.Filters)
{
    if (filter is ErrorLogFilter) // Static analysis tells me this can never be an ErrorLogFilter
    {
        // want to execute the filter
    }
}

ErrorLogFilter is added to the global filters like this:

GlobalFilters.Filters.Add(new ErrorLogFilter(provider));

ErrorLogFilter (as part of Elfar, similar to Elmah, but for MVC) is defined as:

public class ErrorLogFilter : FilterAttribute, IExceptionFilter

Any suggestions on how to accomplish this?

هل كانت مفيدة؟

المحلول

GlobalFilters.Filters is a GlobalFilterCollection which contains Filter's which are wrappers around the added IMvcFilters.

Try this instead:

foreach (var filter in GlobalFilters.Filters)
{
    if (filter.Instance is ErrorLogFilter)
    {
        // want to execute the filter
    }
}

So Reshaper is correct signaled that there is no ErrorLogFilter in GlobalFilters.Filters.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top