从 Elmah 错误邮件中过滤已发出信号的异常

发布于 2024-09-13 01:15:23 字数 391 浏览 2 评论 0原文

我已经围绕 Elmah 的错误信号实现了一个包装方法,其中引发的异常只能由 Elmah 在日志记录和邮件发送中看到,但现在我只想从邮件中过滤掉这些信号异常,但仍然记录它们。我该怎么做?

这是我的简单包装器:

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

我想过将输入异常包装在自定义 SignalledException 中,并从邮件中过滤掉这些异常,但随后我的日志充满了 SignalledException,而不是真正的异常。

还有其他想法吗?

I've implemented a wrapper method around Elmah's Error Signaling, where a raised exception is only seen by Elmah for logging and mailing, but now I'd like to filter only these signalled exceptions out of the mailing, but still log them. How can I do this?

This is my simple wrapper:

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

I've thought of wrapping the input exception in a custom SignalledException, and filtering those out of mailing, but then my logs get full of SignalledExceptions, and not the real exceptions.

Any other ideas please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-09-20 01:15:23

Elmah 过滤主要针对异常类型。编程过滤允许我检查添加到异常的信息,但我还没有遇到带有 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) {}
}

然后,当我引发异常时,我通常只想记录并且不是通过电子邮件发送,但有时可能通过电子邮件发送,我在异常日志记录服务中使用此代码:

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

Elmah filtering works mainly on exception types. Programmatic filtering would allow me to examine information added to an exception, but I have yet to come across an Exception type with an ElmahFilteringData property, and I prefer not to 'invade' the exceptions I want logged. Here is how I accomplished only sending email notificatins for certain signalled exceptions:

First I have a special wrapper exception, purely for telling Elmah not to send an email notification for exceptions of this type:

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

Then, when I raise an exception I normally only want logged and not emailed, but maybe sometimes emailed, I use this code in my exception logging service:

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

Now, in the Elmah filter events in Global.asax, I unwrap the exception to log it, and if it is wrapped, dismiss it from the email notification pipeline:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文