0

I am working on a MVC3 razor application. I have created a error Handling functionality to log the un-handled exceptions. As bellow:

public class ErrorHandlingAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {           
        base.OnException(context);
        LogException(context);
    }
}

On each controller I just need to add the error handler as bellow:

[ErrorHandlingAttribute]
public class HomeController : Controller  

It logging the errors but some how its hitting the OnException method twice. And then it writes the duplicate log.

Can anyone suggest me what its happening.

Many thanks

user1211185
  • 731
  • 3
  • 12
  • 27
  • Perhaps an exception in your HandleError ? And why you named it like the default class ? – Dragouf Jun 20 '12 at 09:13
  • No There is no other exception. I have set the breakpoints and it logging the same exception twice. I have renamed it to 'ErrorHandlingAttribute'. thanks – user1211185 Jun 20 '12 at 09:23
  • 1
    Did you disable the default `HandleError` attribute which is added as a global action filter in Global.asax? – Darin Dimitrov Jun 20 '12 at 09:25
  • @DarinDimitrov it worked. I just commented it out and it worked. Thanks – user1211185 Jun 20 '12 at 09:47
  • Can you please suggest me which one I need to keep it there in global. `ErrorHandlingAttribute` or `HandleErrorAttribute`? – user1211185 Jun 20 '12 at 09:49
  • You need to keep the one that you want to be used. If you put yours, then you no longer need to decorate your controllers with it because it will automatically apply to all your controllers. I have posted my previous comment as answer. – Darin Dimitrov Jun 20 '12 at 09:50

1 Answers1

2

You should remove the default HandleError attribute that is registered by default in your Global.asax. You could replace it with your custom attribute.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    With `HandleError` enabled and custom attribute disabled it write the exception log only once. but If I replace the `HandleError` with custom attribute it writes twice. – user1211185 Jun 20 '12 at 10:03
  • 2
    Probably because your custom exception attribute derives from HandleErrorAttribute :-) If you don't want the basic functionality you could derive your custom attribute directly from `FilterAttribute` and implement the `IExceptionFilter` interface. – Darin Dimitrov Jun 20 '12 at 10:04