【问题标题】:ELMAH filtering not workingELMAH 过滤不起作用
【发布时间】:2010-11-15 21:50:58
【问题描述】:

我的 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,因此过滤器应该匹配,但它似乎根本不起作用。

我做错了什么?

【问题讨论】:

  • 原来状态码是200……为什么?
  • 响应状态码为 200,但异常显示:“在控制器 'controllernamehere' 上找不到公共操作方法 'Register3'。”

标签: asp.net filtering elmah


【解决方案1】:

找到了答案。 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();
}

【讨论】:

  • 谢谢你,我遇到了同样的问题,你的帖子有帮助。不过出于兴趣,我可能在这里很密集,但是 if 语句的组合让我感到困惑,因为第二个 if 正在检查所有 HttpExceptions 并解雇,所以第一个声明将毫无意义,因为无论如何都会解雇异常?谢谢保罗
  • 保利,你是对的。我可能不应该在第二个 if 语句中包含 HttpException,因为我只想忽略状态代码为 404 的特定 HttpException。
猜你喜欢
  • 1970-01-01
  • 2017-02-19
  • 2016-08-24
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多