1

如何配置 EmailTraceListener 以避免邮箱泛滥(大量问题)?

是否可以使用日志记录应用程序块配置设置发送消息的最大值(每小时/每天)?

PS在调用WriteLog之前我已经在一些条件下完成了它,但是想将所有这些东西移到配置中......

4

2 回答 2

2

看起来目前是不可能的

使用电子邮件跟踪侦听器的当前实现,您无法限制发送的邮件数量。但是您可以修改电子邮件跟踪侦听器源代码以在达到一定数量时停止发送。您还可以使用该行为实现自定义跟踪侦听器。

于 2010-11-24T15:11:56.040 回答
1

好的。我写了这个解决方案,对我来说就足够了(每天最多 200 个):

public class MyEmailTraceListener : EmailTraceListener
{
    public const int MAXPER24HOURS = 200;
    public static int counter =0;
    public static DateTime counterReStarted = DateTime.Today;


    private static bool CanLog()
    {
        bool returnValue = false;
        DateTime today = DateTime.Today;
        if (counter < MAXPER24HOURS)
        {
            counter++;
            returnValue=true;
        }
        else if (today>counterReStarted)
        {
            counter = 0;
            counterReStarted = today;
            returnValue = true;
        }
        return returnValue;
    }


    public MyEmailTraceListener(string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer,  int id, ILogFormatter formatter)
        :base(toAddress,        fromAddress,         subjectLineStarter,       subjectLineEnder, smtpServer, id, formatter)
    {
    }

    public MyEmailTraceListener()
    {
    }

    public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
    {
        if (CanLog())
            base.TraceData(eventCache, source, eventType, id, data);
    } 
}

public class MyTraceListenerAssembler : EmailTraceListenerAssembler 
{
    public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
    {
        MyEmailTraceListenerData data = (MyEmailTraceListenerData)objectConfiguration;
        return new MyEmailTraceListener(data.ToAddress, data.FromAddress, data.SubjectLineStarter, data.SubjectLineEnder, data.SmtpServer, data.SmtpPort, base.GetFormatter(context, data.Formatter, configurationSource, reflectionCache));
    }
}

[Assembler(typeof(MyTraceListenerAssembler))]
public class MyEmailTraceListenerData : EmailTraceListenerData
{
}
于 2010-11-24T20:00:07.907 回答