ELMAH 过滤不起作用

发布于 2024-07-30 01:10:58 字数 3233 浏览 6 评论 0原文

我的 web.config 中有以下设置:

<configSections>
    <sectionGroup name="elmah">
        <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
        <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
        <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
        <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
</configSections>

<elmah>
    <security allowRemoteAccess="0" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnHere" />
</elmah>

<system.web>
    <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />            
        <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
</system.web>

我的 global.asax 文件中有以下设置:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

private void Filter(ExceptionFilterEventArgs e)
{
    var context = e.Context as HttpContext;

    if (context != null && context.Response.StatusCode == 404)
        e.Dismiss();

    if (e.Exception.GetBaseException() is FileNotFoundException ||
        e.Exception.GetBaseException() is HttpRequestValidationException)
        e.Dismiss();
}

但每次,Elmah 都会记录 404 异常。 我正在使用 ASP.NET MVC; 它们显示为 System.Web.HttpException 类型,而不是 FileNotFound 异常,但状态代码仍然是 404,因此过滤器应该匹配,但它似乎根本不起作用。

我究竟做错了什么?

I have the following settings in my web.config:

<configSections>
    <sectionGroup name="elmah">
        <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
        <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
        <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
        <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
</configSections>

<elmah>
    <security allowRemoteAccess="0" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnHere" />
</elmah>

<system.web>
    <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />            
        <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
</system.web>

And the following in my global.asax file:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

private void Filter(ExceptionFilterEventArgs e)
{
    var context = e.Context as HttpContext;

    if (context != null && context.Response.StatusCode == 404)
        e.Dismiss();

    if (e.Exception.GetBaseException() is FileNotFoundException ||
        e.Exception.GetBaseException() is HttpRequestValidationException)
        e.Dismiss();
}

And yet every single time, Elmah logs 404 exceptions. I'm using ASP.NET MVC; they show up as type System.Web.HttpException, not FileNotFound exception, but the status code is still 404 and so the filter should match, but it doesn't appear to be working at all.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-08-06 01:10:58

找到了答案。 Filter 方法需要检查 HttpException.GetHttpCode() 方法的结果,而不是检查 Response.StatusCode 属性。

private void Filter(ExceptionFilterEventArgs e)
{
    var exception = e.Exception.GetBaseException();
    var httpException = exception as HttpException;

    if (httpException != null && 
        httpException.GetHttpCode() == 404)
        e.Dismiss();

    if (exception is FileNotFoundException ||
        exception is HttpRequestValidationException ||
        exception is HttpException)
        e.Dismiss();
}

Found the answer. The Filter method needs to check the result of the HttpException.GetHttpCode() method, rather than checking the Response.StatusCode property.

private void Filter(ExceptionFilterEventArgs e)
{
    var exception = e.Exception.GetBaseException();
    var httpException = exception as HttpException;

    if (httpException != null && 
        httpException.GetHttpCode() == 404)
        e.Dismiss();

    if (exception is FileNotFoundException ||
        exception is HttpRequestValidationException ||
        exception is HttpException)
        e.Dismiss();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文