我实现了各地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