This is exactly what I needed for my MVC site configuration!
I added a little modification to the OnException
method to handle multiple HandleErrorAttribute
instances, as suggested by Atif Aziz:
bear in mind that you may have to take care that if multiple
HandleErrorAttribute
instances are in effect then duplicate logging does not occur.
I simply check context.ExceptionHandled
before invoking the base class, just to know if someone else handled the exception before current handler.
It works for me and I post the code in case someone else needs it and to ask if anyone knows if I overlooked anything.
Hope it is useful:
public override void OnException(ExceptionContext context)
{
bool exceptionHandledByPreviousHandler = context.ExceptionHandled;
base.OnException(context);
Exception e = context.Exception;
if (exceptionHandledByPreviousHandler
|| !context.ExceptionHandled // if unhandled, will be logged anyhow
|| RaiseErrorSignal(e) // prefer signaling, if possible
|| IsFiltered(context)) // filtered?
return;
LogException(e);
}