Asp.net MVC 3 500 Error logging not working on ser

2019-09-02 10:17发布

问题:

I have configured an error 500 page with a controller an a view to display when a 500 occures via the webconfig.

   <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404" />
        <remove statusCode="500" />
        <error statusCode="404" responseMode="ExecuteURL" path="/error/404" />
        <error statusCode="500" responseMode="ExecuteURL" path="/error/500" />
    </httpErrors>

In the application_error event i log the error

    protected void Application_Error(object sender, EventArgs e)
    {
        var objErr = Server.GetLastError().GetBaseException();
        var err = string.Concat("Error in: ", Request.Url, "\nMessage:", objErr.Message, "\nStack Trace:", objErr.StackTrace);
        DatabaseLogging.WriteLog(err, 3);
    }

On my dev envirement it works perfect, the exception is logged and the error page is shown. On the acceptance envirment the 500 error page is show, but the real error is not logged. The following error gets logged.

Message:The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:  ~/Views/User/Error.aspx  ~/Views/User/Error.ascx  ~/Views/Shared/Error.aspx  ~/Views/Shared/Error.ascx  ~/Views/User/Error.cshtml  ~/Views/User/Error.vbhtml  ~/Views/Shared/Error.cshtml  ~/Views/Shared/Error.vbhtml Stack Trace:   at System.Web.Mvc.ViewResult.FindView(ControllerContext context)     at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)     at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)     at System.Web.Mvc.Controller.ExecuteCore()     at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)     at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()     at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()     at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Anyone got an idea how I can solve this problem? the following routes are mapped:

        // Error pages
        routes.MapRoute("Error404", "error/404", new { controller = "Error", action = "Error404" });
        routes.MapRoute("Error500", "error/500", new { controller = "Error", action = "Error500" });

回答1:

I guess the MVC HandleError attribute is jumping in here, which is enabled by default for all actions by installing a global filter.

You have not shown the <customErrors mode="?" part of your web.config. I guess its set to mode="remoteOnly", which would explain the behaviour you see.

When running locally on your dev machine, the customErrors handling of ASP.NET is bypassed when setting customErrors mode="remoteOnly" and the <httpErrors element which is part of the IIS config (not ASP.NET) gets into action and displays the page corresponding to the http status code you configured.

When running on a remote machine, ASP.NET error handling is active and the HandleError attribute is looking for the default error view which is called Error to display.

If you don't have a view called Error ASP.NET MVC raises an exception, that it is not able to find the configured error view.

The simplest solution in your case is to remove the globally installed HandleError filter by removing the following line in global.asax.cs:

filters.Add(new HandleErrorAttribute());


回答2:

Remove the following line from your global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // filters.Add(new HandleErrorAttribute()); <!-- remove this
}