为什么我在ASP.NET MVC中使用ELMAH获取重复的异常条目?

时间:2010-02-25 15:49:45

标签: asp.net-mvc url-routing elmah

我是ELMAH的新手,但我现在已经和MVC合作了一段时间。在阅读了关于这个主题的几个博客之后,我正在寻找一个处理404和未知错误页面的ErrorController,并制定一个默认路由,将所有未知路径转发到该控制器上的404操作。

问题是 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();
    }
}

1 个答案:

答案 0 :(得分: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" }
);

冗余路由是否可能是错误日志中双重条目的原因?