为什么我在 ASP.NET MVC 中使用 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();
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么必须放弃默认路由?
我看到您正在定义一个
ErrorDefault
路由和一个 catchAll 路由,这对我来说似乎是多余的。您希望 catchAll 路由处理任何未知路由,因此您需要在其中定义错误。您可以尝试以下操作:
冗余路由是否是错误日志中重复条目的原因?
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:
Could the redundant route be the cause of the double entries in the error log?