ELMAH - 使用自定义错误页面收集用户反馈

发布于 2024-08-19 03:29:57 字数 1998 浏览 4 评论 0原文

我正在第一次考虑使用 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 有可能有多个请求同时出现错误。但这种情况发生的可能性很小。而且这个解决方案独立于会话,这在我们的例子中很重要,而且一些错误也可能导致会话终止等。因此,为什么这种方法对我们来说效果很好。

I'm looking at using ELMAH for the first time but have a requirement that needs to be met that I'm not sure how to go about achieving...

Basically, I am going to configure ELMAH to work under asp.net MVC and get it to log errors to the database when they occur. On top of this I be using customErrors to direct the user to a friendly message page when an error occurs. Fairly standard stuff...

The requirement is that on this custom error page I have a form which enables to user to provide extra information if they wish. Now the problem arises due to the fact that at this point the error is already logged and I need to associate the loged error with the users feedback.

Normally, if I was using my own custom implementation, after I log the error I would pass through the ID of the error to the custom error page so that an association can be made. But because of the way that ELMAH works, I don't think the same is quite possible.

Hence I was wondering how people thought that one might go about doing this....

Cheers

UPDATE:

My solution to the problem is as follows:

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

Then when the error occurs, I have something like this in my 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;
} 

Then where ever you are later you can pull out the details by

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

Note this isn't 100% perfect as its possible for the same IP to have multiple requests to have errors at the same time. But the likely hood of that happening is remote. And this solution is independent of the session, which in our case is important, also some errors can cause sessions to be terminated, etc. Hence why this approach has worked nicely for us.

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

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

发布评论

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

评论(1

天冷不及心凉 2024-08-26 03:29:57

ErrorLogModule提供了 Logged 事件,您可以在 Global.asax 中处理该事件,并可使用该事件来传达详细信息,通过 HttpContext.Items< 说/a> 集合,到您的自定义错误页面。如果您在 web.config 中以 ErrorLog 名称注册了 ErrorLogModule,则 Global.asax 中的事件处理程序将看起来像这样:

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)  
{ 
    var id = args.Entry.Id
    // ...  
}

The ErrorLogModule in ELMAH (version 1.1 as of this writing) provides a Logged event that you can handle in Global.asax and which you can use to communicate details, say via HttpContext.Items collection, to your custom error page. If you registered the ErrorLogModule under the name ErrorLog in web.config then your event handler in Global.asax will look like this:

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)  
{ 
    var id = args.Entry.Id
    // ...  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文