ELMAH - 使用自定义错误页面收集用户反馈
我正在第一次考虑使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ErrorLogModule
提供了
Logged
事件,您可以在Global.asax
中处理该事件,并可使用该事件来传达详细信息,通过HttpContext.Items
< 说/a> 集合,到您的自定义错误页面。如果您在web.config
中以ErrorLog
名称注册了ErrorLogModule
,则Global.asax
中的事件处理程序将看起来像这样:The
ErrorLogModule
in ELMAH (version 1.1 as of this writing) provides aLogged
event that you can handle inGlobal.asax
and which you can use to communicate details, say viaHttpContext.Items
collection, to your custom error page. If you registered theErrorLogModule
under the nameErrorLog
inweb.config
then your event handler inGlobal.asax
will look like this: