2009-07-31 77 views
9

我在我的web.config以下設置:ELMAH過濾不工作

<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,因此過濾器應該匹配,但它似乎根本不起作用。

我在做什麼錯?

+0

事實證明,狀態碼爲200 ......爲什麼? – Chris 2009-07-31 19:35:07

+0

響應狀態代碼爲200,但是異常爲:「控制器'controllernamehere'上找不到公共操作方法'Register3'。」 – Chris 2009-07-31 19:36:13

回答

10

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