【问题标题】:Problem passing ELMAH log id to Custom Error page in ASP.NET将 ELMAH 日志 ID 传递到 ASP.NET 中的自定义错误页面时出现问题
【发布时间】:2010-05-21 20:52:29
【问题描述】:

我正在使用ELMAH 在 ASP.NET Webforms 应用程序中记录未处理的异常。日志记录工作正常。

我想将 ELMAH 错误日志 ID 传递给自定义错误页面,该页面将使用户能够通过电子邮件向管理员发送有关错误的信息。我听从了this answer 的建议。这是我的global.asax 代码:

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{        
    Session[StateKeys.ElmahLogId] = args.Entry.Id;

    // this doesn't work either:
    // HttpContext.Current.Items[StateKeys.ElmahLogId] = args.Entry.Id;
}

但是,在自定义错误页面上,会话变量引用和 HttpContext.Current.Items 给了我一个 NullReference 异常。如何将 ID 传递到我的自定义错误页面?

【问题讨论】:

    标签: c# .net visual-studio elmah


    【解决方案1】:

    这对我有用:

    void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
    {
        if (args.Entry.Error.Exception is HandledElmahException)
            return;
    
        var config = WebConfigurationManager.OpenWebConfiguration("~");
        var customErrorsSection = (CustomErrorsSection)config.GetSection("system.web/customErrors");        
    
        if (customErrorsSection != null)
        {
            switch (customErrorsSection.Mode)
            {
                case CustomErrorsMode.Off:
                    break;
                case CustomErrorsMode.On:
                    FriendlyErrorTransfer(args.Entry.Id, customErrorsSection.DefaultRedirect);
                    break;
                case CustomErrorsMode.RemoteOnly:
                    if (!HttpContext.Current.Request.IsLocal)
                        FriendlyErrorTransfer(args.Entry.Id, customErrorsSection.DefaultRedirect);
                    break;
                default:
                    break;
            }
        }        
    }
    
    void FriendlyErrorTransfer(string emlahId, string url)
    {
        Server.Transfer(String.Format("{0}?id={1}", url, Server.UrlEncode(emlahId)));
    }
    

    【讨论】:

    • 其中HandledElmahException 是您的答案here 中描述的自定义类
    • 你有没有收到“错误执行子请求...”Server.Transfer
    【解决方案2】:

    无法评论 Ronnie 的解决方案。我已经有一段时间了,但它破坏了标准错误流程并导致 ErrorLog_Logged 始终传输,即使在调用时也是如此

    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

    如果您仍想在 catch 语句中记录错误,这将是一个问题,例如,您有错误的解决方法,但希望记录错误以执行正确的修复,这对于难以处理的问题非常有用复制问题。

    我能够通过使用以下更改来纠正此问题:

    //if (customErrorsSection != null)    
    if (customErrorsSection != null && this.Context.Error != null)
    

    这正确地尊重了典型的错误处理,因为 context.Error 在你在 Elmah 中明确引发异常的情况下将为 null,但在通过默认错误处理时不为 null(不是通过 catch 或如果被捕获并重新-抛出)。这会导致 Ronnie 的解决方案响应类似于 .Net 错误处理逻辑。

    【讨论】:

    • 我很久以前就停止使用 Elmah。自己记录错误更容易。
    • 是的,我也不知道我是否会再次沿着 Elmah 路走下去。它很方便,因为它无需大量工作就可以很好地展示或堆栈跟踪,但它通常让我想要扩展它,更改数据结构和其他通常不值得付出努力的事情。
    • 获取堆栈跟踪并不比调用 exception.ToString() 更容易...包括异常类型、消息和调用堆栈并通过内部递归。
    • 通过在 MVC5 中抛出一个新的 Exception("blah") 对此进行了测试,并且 this.Context.Error 为空。不幸的是,如果能够引发错误而不必像接受的答案那样将它们包装在不同的异常中,那就太好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2011-05-01
    • 2013-05-18
    相关资源
    最近更新 更多