加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ELMAH – 使用自定义错误页面收集用户反馈

发布时间:2020-12-15 22:30:12 所属栏目:asp.Net 来源:网络整理
导读:我正在寻找第一次使用ELMAH,但有一个需要满足的要求,我不知道如何实现…… 基本上,我将配置ELMAH在asp.net MVC下工作,并让它在发生错误时将错误记录到数据库中.除此之外,我将使用customErrors在发生错误时将用户定向到友好的消息页面.相当标准的东西…… 要
我正在寻找第一次使用ELMAH,但有一个需要满足的要求,我不知道如何实现……

基本上,我将配置ELMAH在asp.net MVC下工作,并让它在发生错误时将错误记录到数据库中.除此之外,我将使用customErrors在发生错误时将用户定向到友好的消息页面.相当标准的东西……

要求是在这个自定义错误页面上我有一个表单,使用户可以根据需要提供额外的信息.现在问题出现的原因是此时已经记录了错误,我需要将漏洞错误与用户反馈相关联.

通常,如果我使用自己的自定义实现,在记录错误后,我会将错误的ID传递给自定义错误页面,以便可以建立关联.但是由于ELMAH的工作方式,我认为不太可能.

因此,我想知道人们怎么会想到这样做……

干杯

更新:

我对这个问题的解决方案如下:

public class UserCurrentConextUsingWebContext : IUserCurrentConext
{
    private const string _StoredExceptionName = "System.StoredException.";
    private const string _StoredExceptionIdName = "System.StoredExceptionId.";

    public virtual string UniqueAddress
    {
        get { return HttpContext.Current.Request.UserHostAddress; }
    }

    public Exception StoredException
    {
        get { return HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] as Exception; }
        set { HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] = value; }
    }

    public string StoredExceptionId
    {
        get { return HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] as string; }
        set { HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] = value; }
    }
}

然后当错误发生时,我的Global.asax中有类似的东西:

public void ErrorLog_Logged(object sender,ErrorLoggedEventArgs args)
{
    var item = new UserCurrentConextUsingWebContext();
    item.StoredException = args.Entry.Error.Exception;
    item.StoredExceptionId = args.Entry.Id;
}

那么你以后哪里可以提取细节

var item = new UserCurrentConextUsingWebContext();
    var error = item.StoredException;
    var errorId = item.StoredExceptionId;
    item.StoredException = null;
    item.StoredExceptionId = null;

请注意,这不是100%完美,因为同一IP可能同时有多个请求有错误.但发生这种情况的可能性很小.而且这个解决方案独立于会话,在我们的情况下很重要,一些错误也会导致会话被终止等等.因此,为什么这种方法对我们很有效.

解决方法

ELMAH中的 ErrorLogModule(撰写本文时为1.1版)提供了一个 Logged事件,您可以在Global.asax中处理该事件,您可以使用该事件将详细信息(例如通过 HttpContext.Items集合)传递到您的自定义错误页面.如果您在web.config中以ErrorLog的名称注册了ErrorLogModule,那么Global.asax中的事件处理程序将如下所示:
void ErrorLog_Logged(object sender,ErrorLoggedEventArgs args)  
{ 
    var id = args.Entry.Id
    // ...  
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读