【问题标题】:Elmah not logging exceptionsElmah 没有记录异常
【发布时间】:2011-06-04 16:25:58
【问题描述】:

在我的 MVC 网络项目中。我正在尝试在 web.config 中不使用“custromerrors”元素的情况下向访问者显示自定义错误页面。

我可以捕获如下异常

protected void Application_Error(object sender, EventArgs e)
{

    Exception exception = Server.GetLastError();

    bool success = RaiseErrorSignal(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                routeData.Values.Add("action", "Error404");
                break;
            case 500:
                // Server error.
                routeData.Values.Add("action", "Error500");
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template  
            default:
                routeData.Values.Add("action", "Index");
                break;
        }
    }

    // Pass exception details to the target error View.
    routeData.Values.Add("error", exception);

    // Clear the error on server.
    Server.ClearError();

    // Call target Controller and pass the routeData.
    IController errorController = new ProjectName.WebSite.Controllers.ErrorController();
    errorController.Execute(new RequestContext(
         new HttpContextWrapper(Context), routeData));


}

private static bool RaiseErrorSignal(Exception e)
{
    var context = HttpContext.Current;
    if (context == null)
        return false;
    var signal = ErrorSignal.FromContext(context);
    if (signal == null)
        return false;
    signal.Raise(e, context);
    return true;
}

但是 Elmah 不能记录错误,我也提出了错误信号。

【问题讨论】:

  • 那么,您的问题是什么?我看到的只是一个结论。
  • signal.Raise(e,context) 不起作用,因为当我去 /elmah.axd 查看异常时,列表为空。

标签: c# asp.net-mvc error-handling elmah


【解决方案1】:

我发现了问题,我错过了 web.config 部分。我将“ErrorLog”模块添加到<system.webserver><modules>

我还需要添加到<system.web><httpModules>

添加后,Elmah 开始记录错误。

另外我不需要调用 ErrorSignal.Raise() 方法,Elmah 可以在不发出信号的情况下检测错误。

【讨论】:

  • 另一个注意事项:在我的情况下,这是 App_Data 文件夹的写入权限不正确
猜你喜欢
  • 1970-01-01
  • 2011-03-01
  • 2019-03-02
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 2011-01-07
  • 2015-11-11
相关资源
最近更新 更多