【问题标题】:ELMAH not logging exceptions if Custom error page is enabled如果启用自定义错误页面,ELMAH 不会记录异常
【发布时间】:2019-01-02 09:06:13
【问题描述】:

我有这个路径 /test/account 并且它有一个代码

throw new Exception("error");

在我的 web.config 中

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

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

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

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

【问题讨论】:

    标签: asp.net-mvc elmah


    【解决方案1】:

    自定义错误页面实际上会“吞下”异常,因此,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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2010-10-20
      • 2020-07-09
      • 2011-12-24
      • 2010-11-21
      • 2013-05-18
      • 2011-05-22
      相关资源
      最近更新 更多