TEst16
TEst16

Reputation: 407

Error handling and logging in ASP.NET

I have the following requirements for a web app, that consist of MVC 5 pages and WebApi 2 services:

  1. All errors (404, 500, "A potentially dangerous Request.Form value was detected from the client") should produce corresponding error pages in place (no browser redirect, keep the URL the user entered in the address bar).

  2. All error pages should be rendered using dynamic MVC views and contain a unique ID, that the user can give to phone support.

  3. All errors must be logged, the log entries should contain the Id given to the user.

  4. Produce different error pages for different exceptions, e.g. /error/NoSignal for NoSignalException.

Is this doable?

Upvotes: 0

Views: 153

Answers (1)

Aleksej Vasinov
Aleksej Vasinov

Reputation: 2797

I would override exception hangling method - in each controller or in base (depends on your decision).

    protected override void OnException(ExceptionContext filterContext)
    {
        filterContext.Exception.ToString(); // - make any checks here

        // If method is not implemented
        if (filterContext.Exception.GetType() == typeof(NotImplementedException))
        {
            filterContext.Result = new ViewResult { ViewName = "NotImplemented" };
            filterContext.ExceptionHandled = true;
        }
    }

Upvotes: 1

Related Questions