ELMAH - 使用自定义错误页面收集用户反馈 [英] ELMAH - Using custom error pages to collecting user feedback
问题描述
我正在考虑第一次使用 ELMAH,但有一个需要满足的要求,我不确定如何实现...
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...
基本上,我将配置 ELMAH 以在 asp.net MVC 下工作,并在错误发生时将其记录到数据库中.最重要的是,当发生错误时,我使用 customErrors 将用户引导到友好的消息页面.相当标准的东西...
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.
通常,如果我使用自己的自定义实现,在我记录错误后,我会将错误的 ID 传递到自定义错误页面,以便可以进行关联.但由于 ELMAH 的工作方式,我认为不太可能.
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....
干杯
更新:
我对问题的解决方法如下:
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; }
}
}
然后当错误发生时,我的 Global.asax 中有这样的内容:
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;
请注意,这并不是 100% 完美的,因为同一个 IP 可能会同时有多个请求出错.但这种情况发生的可能性很小.而且这个解决方案独立于会话,这在我们的例子中很重要,还有一些错误会导致会话终止等.因此,为什么这种方法对我们来说效果很好.
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.
推荐答案
ErrorLogModule
提供了一个 Logged
事件,您可以在 Global.asax
,您可以使用它来传达详细信息,例如通过 HttpContext.Items
集合,到您的自定义错误页面.如果您在 web.config
中以 ErrorLog
名称注册了 ErrorLogModule
,那么您在 Global.asax
中的事件处理程序将看起来像这样:
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
// ...
}
这篇关于ELMAH - 使用自定义错误页面收集用户反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!