1

How to configure EmailTraceListener to avoid over-flooded mailbox (on mass problem)?

Is it possible to setup a maximum for messages sent (per hour/per day) using logging applicaiton block configuration?

P.S. I have done it already with some conditions before calling WriteLog, but would like to move all this stuff to the configuration...

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142

2 Answers2

2

Looks like it is currently not possible.

With the current implementation of the email tracelistener there is no way you can limit the number of mail sent. But you can modify the email trace listener source code to stop the sending when a certain number is reached. You also can implement your custom trace listener with that behavior.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • @Roman It is not possible in the default EmailTraceListener...you have implemented your own as the last line of that quote states (which may be the route the OP takes so that is of benefit for sure). – Aaron McIver Nov 24 '10 at 20:04
  • I understand what you are saying, but still can't agree that I've implemented custom listener. :) – Roman Pokrovskij Nov 24 '10 at 22:32
1

OK. I wrote this solution, and for me it is enough (max 200 per day):

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
{
}
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
  • 1
    another idea, maximum 10 mails by Hour, and if sendmore mails, group the messages in one email, each hour. – Kiquenet Nov 25 '10 at 11:58