限制电子邮件发送的通过ELMAH数 [英] Limiting the Number of Emails Sent By Elmah

查看:132
本文介绍了限制电子邮件发送的通过ELMAH数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道的一个很好的方式来限制像你可以与健康监测?在一段时间内通过ELMAH发送的电子邮件数量

Does anyone know of a good way to limit the number of emails sent by Elmah during a time period like you can with Health Monitoring?

欲能够从每个页面的每个错误的电子邮件限制为仅一个电子邮件一次一小时左右该特定错误和页

I want to be able to limit the emails for each error from each page to only an email once an hour or so for that particular error and page.

在ELMAH文档,它看起来像使用看:

Looking at the elmah documentation it looks like using:

void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    // perform filtering here   
}

在global.ascx文件可能是一种选择。我可以设置每个应用程序的静态对象,它包含了一些错误的详细信息和时间记录和检查,并取消了电子邮件通知,如果需要的?

in the global.ascx file might be an option. I could setup a static object per application that contains some the error details and the time logged and check it and cancel the email notification if need be?

做任何人有他们现在使用什么更好的解决方案或一个例子吗?

Do anyone have a better solution or an example of what they are using now?

推荐答案

我写这篇用同样的方法在你的问题。似乎很好地工作。

I wrote this using the same method as in your question. Seems to work nicely.

public static DateTime  RoundUp(this DateTime dt, TimeSpan d)
{
    return new DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);
}
static ConcurrentDictionary<int, KeyValuePair<DateTime, string>> _concurrent = new ConcurrentDictionary<int, KeyValuePair<DateTime, string>>();

/// <summary>
/// This is an Elmah event used by the elmah engine when sending out emails. It provides an opportunity to weed out 
/// irrelavent emails.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    preventSpammingDigestEmail(e);
}

/// <summary>
/// Prevents spamming by throttling emails to 5 minute intervals.
/// </summary>
/// <param name="e"></param>
private static void preventSpammingDigestEmail(ExceptionFilterEventArgs e)
{
    DateTime roundedTimeStamp = DateTime.Now.RoundUp(TimeSpan.FromMinutes(5));
    string serialisedException = Util.SerializeException(e.Exception);

    var lastRaisedException = new KeyValuePair<DateTime, string>
        (roundedTimeStamp, serialisedException);

    int key = lastRaisedException.GetHashCode();

    bool errorHasAlreadyBeenRaised = _concurrent.ContainsKey(key);

    // If event has already been raised in the last five minutes dont raise again
    if (errorHasAlreadyBeenRaised)
    {
        e.Dismiss();
        return;
    }

    // Record that it has been raised
    _concurrent.TryAdd(key, lastRaisedException);

    // Clean up existing entries
    Task.Factory.StartNew(() =>
        {
            var toRemove =
                _concurrent.Where(pair => pair.Value.Key < DateTime.Now.Date).Select(pair => pair.Key).ToArray();

            foreach (var i in toRemove)
            {
                KeyValuePair<DateTime, string> keyValuePair;
                _concurrent.TryRemove(i, out keyValuePair);
            }
        });
}

这篇关于限制电子邮件发送的通过ELMAH数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆