MVC 3,Elmah和视图'错误'或其主人未找到

时间:2013-04-01 20:24:42

标签: c# asp.net-mvc-3 elmah

我正在使用一个使用MVC 3,elmah和nhibernate的旧网站。 Elmah日志有数千个“视图'错误'或其主人未找到”错误。我认为它掩盖了真正的错误。我无法弄清楚如何让Elmah记录真实的错误。

作为尝试调试的一种方法,我添加了 - 返回RedirectToAction(“noWhere”); - 强制错误。在本地我得到一个.net屏幕,简单地说“处理你的请求时出现异常......”在暂存时我得到YOSOD屏幕,告诉我设置web.config customerrors节点。两者都将客户错误设置为打开。

网络配置包含以下内容:

 <pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>
<httpModules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>


  <customErrors mode="On" defaultRedirect="~/Views/Shared/PageNotFound">
      <error statusCode="404" redirect="~/Views/Shared/PageNotFound" />
      <error statusCode="500" redirect="~/Views/Shared/PageNotFound" />
  </customErrors>

Global.asax有:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
         filters.Add(new ElmahHandleErrorAttribute());
    }

Elmah班有

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute 
{
    public override void OnException(ExceptionContext context)         
    {             
        base.OnException(context);               
        var e = context.Exception;             
        if (!context.ExceptionHandled // if unhandled, will be logged anyhow      
            || RaiseErrorSignal(e)    // prefer signaling, if possible 
            || IsFiltered(context))   // filtered?
            return;               
        LogException(e);         
    }  

并且baseController类具有:

protected ViewResult PageNotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View("PageNotFound", PageViewModelBuilder.UpdateSiteProperties(new PageViewModel()));
    }

    protected ViewResult PageBadRequest()
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return View("PageNotFound", PageViewModelBuilder.UpdateSiteProperties(new PageViewModel()));
    }

任何有关将正确错误记录到日志的帮助都将不胜感激....

1 个答案:

答案 0 :(得分:1)

在您的web.config中将"~/Views/Shared/PageNotFound"更改为"~/Views/Shared/PageNotFound.aspx”(或"~/Views/Shared/PageNotFound.chtml”),并确保PageNotFound.aspx文件夹中有Shared