vdh*_*ant 8 error-handling asp.net-mvc elmah custom-error-pages
我正在寻找第一次使用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; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后当错误发生时,我在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;
}
Run Code Online (Sandbox Code Playgroud)
那么你以后哪里可以提取细节
var item = new UserCurrentConextUsingWebContext();
var error = item.StoredException;
var errorId = item.StoredExceptionId;
item.StoredException = null;
item.StoredExceptionId = null;
Run Code Online (Sandbox Code Playgroud)
请注意,这不是100%完美,因为同一IP可能同时有多个请求有错误.但发生这种情况的可能性很小.而且这个解决方案独立于会话,在我们的情况下这很重要,一些错误也会导致会话被终止等等.因此,为什么这种方法对我们很有效.
将ErrorLogModule在ELMAH(1.1写这篇文章的版本)提供了一个Logged可以处理的事件Global.asax,哪些是你可以用它来沟通信息,通过说HttpContext.Items收藏,您的自定义错误页.如果您ErrorLogModule在名称下注册了ErrorLog,web.config那么您的事件处理程序Global.asax将如下所示:
void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
var id = args.Entry.Id
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3239 次 |
| 最近记录: |