2

We are creating MVC3 applications. We are using default editors and model state validation. We need to log application errors, but we prefer to make it by some kind of a global handler. We have a handler for unhandled exceptions, but we also want to log model state errors.

The question is: Where can we attach our logger to log such errors? Can we somehow override ModelState or detect situation when model served to view has model errors?

Łukasz W.
  • 9,538
  • 5
  • 38
  • 63
  • 1
    Maybe try checking out global filters as shown here: http://stackoverflow.com/questions/4842721/asp-net-mvc-3-handleerror-global-filter-always-shows-iis-status-500-page – Alex H Aug 25 '11 at 08:23
  • Thanks.. Thats nice.. could you put it as an answer, then I'll accept it ;) – Łukasz W. Aug 25 '11 at 08:40

2 Answers2

1

Global filters will most likely be your best way to go. More from SO here: asp.net mvc 3 handleerror global filter always shows IIS status 500 page

Or checkout the msdn doc here: http://msdn.microsoft.com/en-us/library/gg416513(v=vs.98).aspx

Community
  • 1
  • 1
Alex H
  • 733
  • 4
  • 8
-1

Create a attribute to handle error and register it in the controller,

public class ErrorHandlerAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext exceptionContext)
        {
           LogManager.GetLogger("somelogger").Error(exceptionContext.Exception.Message,exceptionContext.Exception);
            base.OnException(exceptionContext);
        }
    }

register it in the controller like this,

[EwmsErrorHandler(ExceptionType = typeof(base type of exception to handle), View = view to redirect)]
public class MyController : Controller
{
<controller code>
}
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43