ASP.NET中的错误处理和日志记录

时间:2014-02-07 19:54:53

标签: asp.net-mvc asp.net-web-api

我对Web应用程序有以下要求,包括MVC 5页面和WebApi 2服务:

  1. 所有错误(404,500,“从客户端检测到潜在危险的Request.Form值”)应该生成相应的错误页面(没有浏览器重定向,保留用户在地址栏中输入的URL )。

  2. 所有错误页面都应使用动态MVC视图呈现,并包含用户可以为电话支持提供的唯一ID。

  3. 必须记录所有错误,日志条目应包含为用户提供的ID。

  4. 针对不同的例外情况生成不同的错误页面,例如/error/NoSignal代表NoSignalException。

  5. 这可行吗?

1 个答案:

答案 0 :(得分:1)

我会覆盖异常挂起方法 - 在每个控制器或基础中(取决于您的决定)。

    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;
        }
    }