【问题标题】:Why am I getting duplicate exception entries using ELMAH in ASP.NET MVC?为什么我在 ASP.NET MVC 中使用 ELMAH 得到重复的异常条目?
【发布时间】:2010-02-25 15:49:45
【问题描述】:

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

【问题讨论】:

  • 您实际在哪里记录错误?
  • 我正在使用错误记录方法,将错误本地存储在 XML 文件中。
  • 很抱歉造成混乱; ErrorController 不会发出任何错误信号,并且我没有使用 HandleError 属性的任何派生。控制器只显示两个基本视图,并适当地设置传出错误代码。
  • 你找到解决方案了吗?我有同样的问题,我的直觉是 web.config 中有一个重复的日志条目

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


【解决方案1】:

为什么要放弃默认路由?
我看到您正在定义一个 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" }
);

可能是冗余路由导致错误日志中出现重复条目​​吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多