2010-08-04 4 views
1

Elmah의 오류 신호 주위에 래퍼 메서드를 구현했습니다. 여기서는 발생한 예외가 로깅과 메일 발송을 위해 Elmah에게만 표시되지만, 지금은 이러한 신호 된 예외 만 필터링하려고합니다. 우편물의, 그러나 여전히 그들을 기록하십시오. 어떻게해야합니까?Elmah 오류 메일에서 신호 예외를 필터링하는 중

이 내 간단한 래퍼입니다 :

public void LogException(Exception exception) 
    { 
     ErrorSignal.FromContext(HttpContext.Current).Raise(exception); 
    } 

나는 사용자 정의 SignalledException에 입력 예외를 랩, 메일 링 밖으로 그 필터링 생각했지만, 다음 내 로그 SignalledExceptions의 전체 얻을 아닌 진짜 예외.

다른 아이디어는 무엇입니까?

답변

3

엘마 필터링은 주로 예외 유형에서 작동합니다. 프로그래밍 방식 필터링을 사용하면 예외에 추가 된 정보를 검토 할 수 있지만 아직 예외 유형이 ElmahFilteringData 인 예외 유형을 발견하지 못했으며, 기록 된 예외를 '침입'하지 않는 것이 좋습니다.

먼저 나는 특별한 래퍼 예외가 순수 ELMAH를 말하는 아니 이러한 유형의 예외에 대한 이메일 알림을 보낼 : 다음

public class ElmahEMailBlockWrapperException: Exception 
{ 
    public const string Explanation = "This is a wrapper exception that will be blocked by Elmah email filtering. The real exception is the InnerException"; 
    public ElmahEMailBlockWrapperException(Exception wrappedException):base(Explanation, wrappedException) {} 
} 

저는 여기에 특정 신호를 예외 이메일 notificatins을 보내는 달성하는 방법이다 나는 예외를 발생시킬 때, 나는 일반적으로 단지 이메일 기록하지 싶지만, 어쩌면 때때로 이메일, 나는 예외 로깅 서비스에이 코드를 사용 : Global.asax에있는 ELMAH 필터 이벤트에

public void LogException(Exception exception, bool includeEmail) 
{    
    if (includeEmail) 
    { 
     ErrorSignal.FromContext(HttpContext.Current).Raise(exception); 
    } 
    else 
    { 
     // Wrap the input exception in a special exception type that the Elmah email filter can block. 
     var wrappedException = new ElmahEMailBlockWrapperException(exception); 
     ErrorSignal.FromContext(HttpContext.Current).Raise(wrappedException); 
    } 
} 

자, excepti 풀기 켜고 로그에 기록한 경우 이메일 알림 파이프 라인에서이를 닫습니다.

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e) 
{ 
    // If the exception was wrapped in a ElmahEMailBlockWrapperException exception to be blocked by the ErrorMail filter, the InnerException 
    // of the the ElmahEMailBlockWrapperException is the real exception to be logged, so we extract it, log it, and dismiss the wrapper. 
    var ebw = e.Exception.GetBaseException() as ElmahEMailBlockWrapperException; 
    if (ebw != null) 
    { 
     ErrorLog.GetDefault(HttpContext.Current).Log(new Error(ebw.InnerException)); 
     e.Dismiss(); 
    } 
} 

public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e) 
{ 
    // If the exception was wrapped, i.e. raised only to be logged by Elmah and not emailed, dismiss it. 
    if (e.Exception.GetBaseException() is ElmahEMailBlockWrapperException) 
    { 
     e.Dismiss(); 
    } 
}