2011-01-06 2 views
6

내 MVC 웹 프로젝트에서. web.config에서 "custromerrors"요소를 사용하지 않고 방문자에게 사용자 정의 오류 페이지를 표시하려고합니다.예외를 로깅하지 않는 Elmah

내가 ELMAH 캔트 로그 오류는 내가 오류 신호를 제기하고

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

이하 그러나 같은 예외를 잡을 수 있습니다.

+2

귀하의 질문은 무엇입니까? 내가 보는 모든 결론입니다. –

+0

signal.Raise (e, context)가 작동하지 않습니다. 예외를보기 위해 /elmah.axd에 갈 때 목록이 비어 있기 때문입니다. – Yucel

답변

9

문제가 발견되어 web.config 섹션을 놓쳤습니다. "ErrorLog"모듈을 <system.webserver><modules>에 추가했습니다.

또한 <system.web><httpModules>에 추가해야합니다.

추가 후, Elmah가 오류를 기록하기 시작합니다.

또한 Elmah는 ErrorSignal.Raise() 메서드를 호출 할 필요가 없으며 신호없이 오류를 검색 할 수 있습니다.

+4

또 다른 참고 사항 : 내 경우에는 App_Data 폴더의 쓰기 권한이 잘못되었습니다. – devqon

관련 문제