【发布时间】:2011-01-09 17:34:12
【问题描述】:
我正在考虑第一次使用 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 可能有多个请求同时出现错误。但发生这种情况的可能性很小。而且这个解决方案独立于会话,这在我们的例子中很重要,一些错误也可能导致会话终止等。因此,为什么这种方法对我们很有效。
【问题讨论】:
-
你让它工作了吗?我有同样的问题。我遵循了 Atif 的建议,但是当我在自定义错误页面上为 Elmah Id 引用 HttpContext.Items 时,我得到了一个空引用异常。
-
@Ronnie Overby:见上面的编辑。
-
谢谢。我想过做类似的事情,但使用 cookie/ipaddress 组合作为标识符。最终我只是这样做了:stackoverflow.com/questions/2885487/…
-
@Ronnie Overby: np...看起来是一种合理的方法...唯一的事情是我们想将异常(这是一个复杂的对象)传递到发生错误的页面.由于该系统是私有系统,如果所有日志记录失败,我们会告诉用户失败并将错误信息打印到页面源中。因此,如果需要,我们可以访问它。
标签: asp.net-mvc error-handling elmah custom-error-pages