代码之家  ›  专栏  ›  技术社区  ›  zsharp

最小错误处理方法

  •  2
  • zsharp  · 技术社区  · 16 年前

    希望获得有关asp.net mvc web应用程序快速简便错误处理的建议。

    4 回复  |  直到 16 年前
        1
  •  4
  •   Community Mohan Dere    8 年前

    ELMAH 与扩展HandleError并通过ELMAH记录错误的属性结合使用。看到这个了吗 question/answer 有关如何使HandleError属性与ELMAH一起工作的一些想法。丹·斯瓦提克也是 blogged

        2
  •  2
  •   JeremyWeir    16 年前

    您仍然可以在Global.asax中使用旧的Application_Error方法

    protected void Application_Error(object sender, EventArgs e)
    {
        // some good info in Server.GetLastError().GetBaseException() and Context.Request
        // that you can use in whatever you choose for 
        // your quick and easy logging/reporting
    }
    

    显然,您也会希望启用customErrors。。。

    <customErrors mode="RemoteOnly" defaultRedirect="~/error">
      <error statusCode="403" redirect="~/error/forbidden" />
      <error statusCode="404" redirect="~/error/notfound" />
      <error statusCode="500" redirect="~/error" />
    </customErrors>
    
        3
  •  1
  •   griegs    16 年前

        4
  •  1
  •   Funka    16 年前

    我最近刚刚使用了一个我自己的快速n脏错误报告解决方案,它可能会给你一些想法。。。

    创建一个基本控制器类,让我们调用它 MyBaseController . 如果愿意,让您的所有控制器都继承自此,但如果您只有一个控制器,则不需要这样做。

    然后你可以覆盖它的部分 OnException 方法并插入任何类型的错误报告,例如向自己发送一封包含异常的ToString()的电子邮件。例如:

    public MyOwnBaseController : Controller
        protected override void OnException(ExceptionContext context)
        {
            SendAdminErrorAlert("Egads! An Error!", context.Exception.ToString());
            // I have a view named "500.aspx" that I will now show...
            context.ExceptionHandled = true;
            this.View("500").ExecuteResult(this.ControllerContext);
        }
    }
    

    http://blog.dantup.me.uk/2009/04/aspnet-mvc-handleerror-attribute-custom.html 在了解所有这些的过程中。。。

    祝你好运
    -f!