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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ELMAH – 使用自定义错误页面收集用户反馈前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找第一次使用ELMAH,但有一个需要满足的要求,我不知道如何实现……

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

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

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

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

干杯

更新:

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

  1. public class UserCurrentConextUsingWebContext : IUserCurrentConext
  2. {
  3. private const string _StoredExceptionName = "System.StoredException.";
  4. private const string _StoredExceptionIdName = "System.StoredExceptionId.";
  5.  
  6. public virtual string UniqueAddress
  7. {
  8. get { return HttpContext.Current.Request.UserHostAddress; }
  9. }
  10.  
  11. public Exception StoredException
  12. {
  13. get { return HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] as Exception; }
  14. set { HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] = value; }
  15. }
  16.  
  17. public string StoredExceptionId
  18. {
  19. get { return HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] as string; }
  20. set { HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] = value; }
  21. }
  22. }

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

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

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

  1. var item = new UserCurrentConextUsingWebContext();
  2. var error = item.StoredException;
  3. var errorId = item.StoredExceptionId;
  4. item.StoredException = null;
  5. item.StoredExceptionId = null;

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

解决方法

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

猜你在找的asp.Net相关文章