gpt4 book ai didi

asp.net-mvc - ASP.NET MVC ELMAH 不记录 HttpRequestValidationExceptions

转载 作者:行者123 更新时间:2023-12-02 02:17:30 24 4
gpt4 key购买 nike

我有一个在 .NET 4.0 上运行的 ASP.NET MVC 站点,我正在尝试设置错误日志记录。

我发现了 Elmah.MVC NuGet 包(v2.1.1,Elmah 核心:v1.2.1)并关注 this tutorial进行设置。 (没有执行Step5 - javascript错误日志记录)

它工作正常,向我发送电子邮件并记录 404 错误,但是当我在输入中输入一些 html 时 <h1>Test</h1>看看网站如何处理它,我得到一个 HttpRequestValidationException ,这很好,因为它不会让他们进入它,并且我有一个错误页面设置,当网站发布时会显示该错误页面设置,但 Elmah 不会记录此详细信息某种错误。

我看过this SO postElmah issue 217说已经解决了。

这是我的 ElmahHandleErrorAttribute 类:

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);

var e = context.Exception;

// Log only handled exceptions, because all other will be caught by ELMAH anyway.
// from http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/
// did nothing
// if (context.ExceptionHandled)
// ErrorSignal.FromCurrentContext().Raise(context.Exception);

if (!context.ExceptionHandled // if unhandled, will be logged anyhow
|| RaiseErrorSignal(e) // prefer signaling, if possible
|| IsFiltered(context)) // filtered?
return;

LogException(e);
}

private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var signal = ErrorSignal.FromContext(context);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}

private static bool IsFiltered(ExceptionContext context)
{
var config = context.HttpContext.GetSection("elmah/errorFilter")
as ErrorFilterConfiguration;

if (config == null)
return false;

var testContext = new ErrorFilterModule.AssertionHelperContext(
context.Exception, HttpContext.Current);

return config.Assertion.Test(testContext);
}

private static void LogException(Exception e)
{
var context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(e, context));
}
}

我在我的 FilterConfig.cs 中引用了这一点:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandleErrorAttribute());
filters.Add(new HandleErrorAttribute());

}

是否有已知的解决方法,以便我可以让 Elmah 记录此类错误的日志和电子邮件详细信息?

谢谢。

最佳答案

此问题似乎与 ASP.NET 4 中的重大更改有关请参阅:this linkthis link

这是我如何让它工作的:

        if (HttpContext.Current != null)
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(e));
else
ErrorSignal.FromCurrentContext().Raise(e);

不知何故,“Elmah.ErrorLog.GetDefault(HttpContext.Current).Log()”方法有效。

关于asp.net-mvc - ASP.NET MVC ELMAH 不记录 HttpRequestValidationExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com