将 ELMAH 日志 ID 传递到 ASP.NET 中的自定义错误页面时出现问题

发布于 2024-09-02 16:05:52 字数 723 浏览 8 评论 0原文

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

我想将 ELMAH 错误日志 ID 传递到自定义错误页面,该页面将使用户能够向管理员发送有关错误的电子邮件。我已遵循此答案。这是我的 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 传递到我的自定义错误页面?

I am using ELMAH to log unhandled exceptions in an ASP.NET Webforms application. Logging is working fine.

I want to pass the ELMAH error log id to a custom error page that will give the user the ability to email an administrator about the error. I have followed the advice from this answer. Here is my global.asax code:

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

But, on the Custom error page, the session variable reference and HttpContext.Current.Items are giving me a NullReference exception. How can I pass the ID to my custom error page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心意如水 2024-09-09 16:05:52

这对我有用:

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

This works for me:

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)));
}
野生奥特曼 2024-09-09 16:05:52

无法评论罗尼的解决方案。我已经这样做了一段时间,但它破坏了标准错误流程并导致 ErrorLog_Logged 始终传输,即使在调用

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

时也是如此如果您仍然想在 catch 语句中记录错误,例如您有错误的解决方法,但希望记录错误以执行正确的修复,这对于难以复制的问题非常有帮助。

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

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

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

Unable to comment on Ronnie's solution. I had that in place for a while but it breaks the standard error flow process and causes ErrorLog_Logged to always transfer, even when calling

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

This is a problem if you still want to log an error from within a catch statement, for instance you have a workaround for an error but want to log the error to perform a proper fix, which can be pretty helpful on difficult to replicate issues.

I was able to correct this by using the following change:

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

This respects the typical error handling properly, as the context.Error will be null in cases where you explicitely raise the exception in Elmah, but is not null when falling through default error handling (not via a catch or if caught and re-thrown). This causes Ronnie's solution to respond similar to the .Net error handling logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文