Asp.net MVC 3 500错误日志记录无法在服务器上运行

时间:2011-12-28 08:48:26

标签: asp.net-mvc-3

我已经配置了一个错误500页的控制器和一个视图,以便在通过webconfig发生500时显示。

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

在application_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);
    }

在我的开发环境中它完美无缺,记录异常并显示错误页面。在接受环境中显示500错误页面,但不记录实际错误。记录以下错误。

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)

任何人都知道如何解决这个问题? 映射了以下路由:

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

2 个答案:

答案 0 :(得分:1)

从global.asax中删除以下行:

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

答案 1 :(得分:1)

我想MVC HandleError属性正在这里跳过,默认情况下通过安装全局过滤器为所有操作启用。

您尚未显示web.config的<customErrors mode="?"部分。我猜它设置为mode="remoteOnly",这可以解释你看到的行为。

在dev机器上本地运行时,在设置customErrors mode="remoteOnly"时会绕过ASP.NET的customErrors处理,并且属于IIS配置(不是ASP.NET)的<httpErrors元素进入操作并显示与您配置的http状态代码对应的页面。

在远程计算机上运行时,ASP.NET错误处理处于活动状态,HandleError属性正在查找要显示的默认错误视图Error

如果您没有名为Error的视图,ASP.NET MVC会引发异常,它无法找到已配置的错误视图。

在您的情况下,最简单的解决方案是通过删除global.asax.cs中的以下行来删除全局安装的HandleError过滤器:

filters.Add(new HandleErrorAttribute());