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>
...
&开发者_开发问答lt;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();
}
}
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?
精彩评论