代码之家  ›  专栏  ›  技术社区  ›  Heero Yuy

如果启用自定义错误页,Elmah不会记录异常

  •  0
  • Heero Yuy  · 技术社区  · 6 年前

    我有这个路径/测试/帐户,它有一个代码

    throw new Exception("error");
    

    在我的web.config中

    <customErrors mode="On">
          <error statusCode="404" redirect="~/404.html" />
        </customErrors>
    

    当它的模式打开时,它会显示自定义错误页而不是异常,并且Elmah不会记录它。当它关闭时,它会显示异常并记录下来。

    我的理解是,我们需要记录异常,并且我们有Elmah用于记录异常,但是我们不想在生产中显示这些详细的异常,所以我们有定制的错误页面。

    我希望即使在出现异常时显示自定义错误页,但仍然希望Elmah记录它。

    1 回复  |  直到 6 年前
        1
  •  0
  •   ThomasArdal    6 年前

    自定义错误页实际上会“吞咽”异常,因此,不会通知Elmah该异常。有一个很好的小技巧记录处理过的异常,从 ELMAH and custom errors . 创建此类:

    public class ElmahExceptionLogger : IExceptionFilter
    {
        public void OnException (ExceptionContext context)
        {
            if (context.ExceptionHandled)
            {
                ErrorSignal.FromCurrentContext().Raise(context.Exception);
            }
        }
    }
    

    然后配置过滤器:

    protected void Application_Start()
    {
        GlobalConfiguration.Configuration.Filters.Add(new ElmahExceptionLogger());
    }