フィルタリングはELMAHエラーメールのうち、例外を合図しました

StackOverflow https://stackoverflow.com/questions/3406648

  •  25-09-2019
  •  | 
  •  

質問

私は、発生した例外のみログおよび郵送のためにELMAHで見られているELMAHのエラーシグナリング、周りのラッパー・メソッドを実装しましたが、今はフィルターに郵送のうち唯一のこれらの合図の例外を好きですが、まだそれらのログを記録したいです。私はこれを行うことができますどのように?

これは私の単純なラッパーであります

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

私は、カスタムSignalledExceptionに入力の例外をラップし、郵送のそれらのうちのフィルタリングを考えてきましたが、その後、私のログはSignalledExceptionsはなく、本当の例外を完全に取得します。

他のアイデアください!

役に立ちましたか?

解決

ELMAHのフィルタリングは、例外の種類に主に動作します。プログラムによるフィルタリングは私が例外に追加された情報を調べることができるようになるが、私はElmahFilteringDataプロパティで例外の種類に遭遇し、まだ持っている、と私は私がログインしたい例外を「侵略」にないことを好みます。ここで私が唯一の特定の合図の例外のために電子メールを送信するnotificatins達成する方法である。

まずは純粋にこのタイプの例外のための電子メール通知を送信しない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) {}
}

そこで、私は例外を発生させたときに、私は正常にのみ記録され、電子メールではない、多分時々電子メールで送信したい、私は私の例外ロギングサービスでこのコードを使用します:

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

さて、Global.asaxの中ELMAHフィルタイベントでは、私はそれをログに記録する例外のラップを解除し、それがラップされている場合は、電子メール通知のパイプラインからそれを却下します:

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();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top