【发布时间】: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