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

发布于 2024-08-23 11:28:37 字数 3308 浏览 6 评论 0原文

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

I'm brand new to ELMAH but I've been working with MVC for a little while now. After reading several blogs on the subject I'm pursuing the road of having an ErrorController that handles 404 and unknown-error pages, and making a default route that forwards all unknown paths to the 404 action on that controller.

The problem is that ELMAH logs every error twice; the detail logs are completely identical except for their identification number specified in brackets in the title.

Has anyone else run into this? The routing seems to work great apart from having to ditch default {controller}/{action}/{id} route.

Here's my configuration:

    <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>

And routing code:

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

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

EDIT: Here's the ErrorController as well, just for my testing purposes:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2024-08-30 11:28:37

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

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

Why do you have to ditch the default route?
I see that you're defining an ErrorDefault route and a catchAll route, which seems redundant to me. You'd want the catchAll route to handle any unknown route so you'd want to define the error in there.

You could try something like:

// 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" }
);

Could the redundant route be the cause of the double entries in the error log?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文