好吧,花了一天的时间来解决这个问题,我想我会分享我的解决方案。与上面 Myster 的解决方案非常相似,不同之处在于我修改了 elmah 使用的错误对象而不是请求服务器变量,如下所示:
public static void LogError(Exception e, IDictionary<string, string> customFields = null)
{
var logger = ErrorLog.GetDefault(HttpContext.Current);
customFields = customFields ?? new Dictionary<string, string>();
// Used to tell elmah not to log this twice (See global.asax)
e.Data.Add("Logged", true);
var error = new Error(e, HttpContext.Current);
customFields.ForEach(pair => error.ServerVariables.Add(pair.Key, pair.Value));
logger.Log(error);
}
然后,根据您的应用程序调用 logger.LogError:
mvc 添加自定义错误过滤器(http://maheshde.blogspot.ca/2012/09/error-handing-with-mvc-using-custom.html)
webforms 覆盖 global.asax 中的 Application_Error
然后,只需要在 global.asax 中的最后一步,消除已记录的所有错误:
public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
if (e.Exception.Data.Contains("Logged"))
{
if (e.Exception.Data["Logged"].Equals(true)) e.Dismiss();
}
}