jef*_*non 1 asp.net error-handling elmah asp.net-mvc-3
我们有一个配置为使用 ELMAH 的 ASP.NET MVC 3 应用程序。我们的 Application_Error 方法中也有这样的代码。ELMAH 和我们的自定义代码都记录到数据库中。
protected void Application_Error(object sender, EventArgs e)
{
MvcApplication app = (MvcApplication)sender;
HttpContext httpContext = app.Context;
Exception ex = app.Server.GetLastError();
HttpException httpException = ex as HttpException;
//log the error with our custom logging
Server.ClearError();
if (httpContext.IsCustomErrorEnabled) //only show custom error if enabled in config
{
httpContext.Response.Clear();
httpContext.ClearError();
//show our own custom error page here
}
}
Run Code Online (Sandbox Code Playgroud)
我们看到的问题(不是真正的问题,但无论如何)是 ELMAH 和我们的自定义代码都将异常记录到数据库中。我希望对 Server.ClearError() 和 httpContext.ClearError 的调用会处理错误,并且它永远不会到达 ELMAH。但是,错误被记录两次的事实是否意味着 ELMAH 和 application_error 基本上并行运行并且它们都同时收到未处理的异常?如果是这样,无论如何要告诉 ELMAH 忽略该错误?
我们的目的是让 ELMAH 仅在出现问题时才处理错误,例如在注册 elmah 之后但在 MVC 应用程序运行之前的 ASP.NET 管道中。
问题是它首先登录到 ELMAH。所以是的,您可以使用以下命令告诉它不要登录到 ELMAH e.Dismiss():ErrorLog_Filtering下面的函数是在之前命中的Application_Error。所以添加这个函数和任何你需要的逻辑来确定你是否想要它在 ELMAH 中。
void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
//get the exceptions like:
Exception m = e.Exception;
Exception ex = e.Exception.GetBaseException();
//tell it not to log the error in ELMAH like (based on whatever criteria you have):
e.Dismiss();
//Application_Error will be hit next
}
protected void Application_Error(object sender, EventArgs e)
{
//your logic
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1708 次 |
| 最近记录: |