2010-02-25 30 views
3

我是ELMAH的新手,但我一直在使用MVC一段時間。在閱讀了關於這個主題的幾篇博文之後,我正在尋求處理404和未知錯誤頁面的ErrorController,並製作一條默認路由,將所有未知路徑轉發到該控制器上的404動作。爲什麼我在ASP.NET MVC中使用ELMAH獲取重複的異常條目?

問題是,ELMAH記錄每個錯誤兩次;詳細記錄除標題中括號內的標識號外完全相同。

有沒有其他人遇到過這個問題?路由似乎工作很大,除了必須排除默認的{controller}/{action}/{id}路由。

這裏是我的配置:

<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> 
    <system.web> 
     ... 
     <customErrors mode="On" defaultRedirect="~/error/unknown/"> 
       <error statusCode="404" redirect="~/error/notfound/"/> 
     </customErrors> 
     ... 
     <httpHandlers> 
     ... 
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/> 
     ... 
</httpHandlers> 
     ... 
     <httpModules> 
    ... 
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> 
     </httpModules> 
    </system.web> 
    <system.webserver> 
      <modules runAllManagedModulesForAllRequests="true"> 
       ... 
       <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> 
      </modules> 
      <handlers> 
       ... 
       <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> 
      </handlers> 
    </system.webserver> 
    <elmah> 
     <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/errorlogpath" /> 
    </elmah> 

和路由代碼:

routes.MapRoute(
     "ErrorDefault", 
     "error/{action}", 
     new { controller = "error", action = "unknown", id = "" } 
     ); 

    routes.MapRoute(
     "Default", 
     "{*url}", 
     new { controller = "error", action = "notfound", id = "" } 
     ); 

編輯:這裏的ErrorController爲好,只是我的測試目的:

/// <summary> 
/// Handles error page routing 
/// </summary> 
public class ErrorController : Controller 
{ 
    /// <summary> 
    /// Action for unknown errors 
    /// </summary> 
    /// <returns></returns> 
    [AcceptVerbs(HttpVerbs.Get)] 
    public ViewResult Unknown() 
    { 
     Response.StatusCode = (int)HttpStatusCode.InternalServerError; 
     return View(); 
    } 

    /// <summary> 
    /// Action for 404s 
    /// </summary> 
    /// <param name="path"></param> 
    /// <returns></returns> 
    [AcceptVerbs(HttpVerbs.Get)] 
    public ViewResult NotFound(string path) 
    { 
     Response.StatusCode = (int)HttpStatusCode.NotFound; 
     return View(); 
    } 
} 
+0

你在哪裏實際記錄錯誤? – 2010-02-25 15:58:42

+0

我正在使用將錯誤本地存儲在XML文件中的錯誤記錄方法。 – 2010-02-25 15:59:53

+0

我的意思是如果ErrorController向Elmah發出錯誤信號,或者ErrorController只顯示一個通用錯誤頁面,並在其他地方發送信號。 – 2010-02-25 16:05:36

回答

0

爲什麼你是否必須拋棄默認路由?
我看到你正在定義一條ErrorDefault路線和一條catchAll路線,這對我來說似乎是多餘的。你需要catchAll路由來處理任何未知的路由,所以你想在那裏定義錯誤。

你可以嘗試這樣的:

// All other pages use the default route. 
routes.MapRoute("Default", "{controller}/{action}/{id}", 
    new { controller = "Applications", action = "Index", id = "" } 
); 

// Show a 404 error page for anything else. 
routes.MapRoute("Error", "{*url}", 
    new { controller = "Error", action = "notfound" } 
); 

能的冗餘路徑是在錯誤日誌中的雙項的原因是什麼?

相關問題