Email TraceListener: subject and sendtime

Topics: Logging Application Block
Aug 9, 2007 at 5:05 PM
Hi,

two problems with the Email-TraceListener:

A) Email-subject
Is there an configuration-option to change the subject für the email sends by Email-Tracelistener? Default seems to be 'Information'. (I dont mean the SubjectLineStarter or SubjectLineEnder. These two I know as optional text...).

B) sendtime of the email
I implemented logging within a windows-service with LAB. A problem is, that the emails will be send only, if the service stops. Emails will not be sended immediately. :-|
Any hints how to send the mail immediately?

Thanks, Axel.
Aug 9, 2007 at 8:05 PM
Hi Axel,



schue wrote:
A) Email-subject
Is there an configuration-option to change the subject für the email sends by Email-Tracelistener? Default seems to be 'Information'. (I dont mean the SubjectLineStarter or SubjectLineEnder. These two I know as optional text...).


The description of the logged entry's severity should be used there.


B) sendtime of the email
I implemented logging within a windows-service with LAB. A problem is, that the emails will be send only, if the service stops. Emails will not be sended immediately. :-|
Any hints how to send the mail immediately?


I'm not sure I follow. Do you mean that the emails are sent only when the service stops (i.e. all the logged messages are queued and when the service stops they are sent)? AFAIK the emails are sent in the thread that is logging using the standard System.Net.Mail.SmtpClient class using the synchronous Send(string) method. Can you describe the behavior you get in more detail, please?

Regards,
Fernando
Aug 9, 2007 at 10:03 PM
Thank you. This helps to deal ith the subject.

For the 2nd problem - you are right, the emails will only be sent when the service stops. It realy seems that the mails are cashed/ queued.
For example the first mail will be sent by Logger.Write(...) in the last line within OnStart(). Some mails will be produced while service running and the last email in last line within OnStop(). And as I see all mails will be sent after stopping the service: my desktop-firewalls scanner for outgoing mail shows, that it now processes all the mails in series. 10 piecies within 1sec - thus normal behavior.

I will give it a try to disable the desktop-firewall. But its not comprehensible why it should queue the mails and start sending when the service stops. Thats why I am not optimistic that the desktop-firewalls scanner is the reason.

Greetings, Axel.
Aug 9, 2007 at 10:03 PM
Edited Aug 9, 2007 at 10:04 PM
double post, sry
Aug 10, 2007 at 3:45 PM
Hi,

I would try a few things to pinpoint the issue:
  • Try sending an email from your service using the raw SmtpClient.SendMail() API. If it works as you expect, you know there is some issue with the logging block; otherwise the issue comes from the platform.
  • Try using a mock, local SMTP server to check the email is being sent when expected or not. Alternatively, temporarily block outgoing SMTP connections in your firewall, and look for error logs from the logging block. That would help you determine if the pause is caused inside your service, or the emails are being held after they were sent.

Regards,
Fernando
Oct 14, 2008 at 2:42 AM
Edited Oct 15, 2008 at 1:07 AM
Hi Axel,


>>>Is there an configuration-option to change the subject für the email sends by Email-Tracelistener? Default seems to be 'Information'. (I dont mean the SubjectLineStarter or SubjectLineEnder. These two I know as optional text...).

Did you have a working solution?

I have tried but it is not working . I rephrased - I changed the subject but it only worked the first time. It does not work the second time.
First time- MyLog (TraceEventType.Error, "General", "Hello world"); // Subject="Error Hello world"
2nd time- MyLog (TraceEventType.Warning, "General", "Beautiful Sydney"); // Subject="Warning Hello world"
3rd time- MyLog (TraceEventType.Information, "General", "Subprime = Black Friday"); // Subject="Information Hello world"

Here is how I attempted to change the subject. If you have any luck, let me know. I would give a few more tries before embarking on my next action is to write a custom trace listerner instead.

In this design, I am using the EmailTraceListenerData.SubjectLineEnder as the msg. I updated EmailTraceListernerData portion in the Configuration on every call.

public void MyLog (TraceEventType severity, string category, string msg)
{
    using (LoggingSubjectScope scope = new LoggingSubjectScope()) // this is not working the second time
    {
        LogEntry logEntry = new LogEntry();
        logEntry.Severity = severity;
        logEntry.Message = msg;
        logEntry.Categories.Add(category);
        scope.SetLogEntry(logEntry);
        Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(logEntry);
    }
}

public class LoggingSubjectScope : IDisposable
{
       public void SetLogEntry(LogEntry entry)
      {
            //Open the default app configuration
            Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            LoggingSettings settings = (LoggingSettings)entLibConfig.GetSection(LOGGING_CONFIG_SECTION);

            bool ok = false;

            foreach (TraceListenerData listener in settings.TraceListeners)
            {
                if (listener is EmailTraceListenerData)
                {
                    string subjectLineStarter;
                    string subjectLineEnder;

                    EmailTraceListenerData emailData = (EmailTraceListenerData)listener;
                    if (!m_savedSubjects.ContainsKey(emailData.Name))
                    {
                        List<String> subjects = new List<String>();
                        subjects.Add(emailData.SubjectLineStarter);
                        subjects.Add(emailData.SubjectLineEnder);
                        m_savedSubjects.Add(emailData.Name, subjects);
                    }
                    emailData.SubjectLineEnder = entry.Message;

 

 

 

                    Debug.WriteLine(emailData.Name + ".SubjectLineStarter = " + emailData.SubjectLineStarter);
                    Debug.WriteLine(emailData.Name + ".SubjectLineEnder = " + emailData.SubjectLineEnder);

                    ok = true;
                }
            }

            if (ok)
            {
                entLibConfig.Save();
            }

       }

        public void Dispose()
        {
            RestoreSubjects();

            GC.SuppressFinalize(this);
        }

        private void RestoreSubjects()
        {
              // ....
        }

        private static Dictionary<String, List<String>> m_savedSubjects = new Dictionary<String, List<String>>();
        const string LOGGING_CONFIG_SECTION = "loggingConfiguration";
}

Oct 14, 2008 at 6:49 AM
Just analysing the behaviour .. it seemed that the TraceListeners are not refreshing their configuration. That is why my above solution is not working. 

I tried also flushing the Logging Block trace listeners but that did not help. (Note 1)
I am getting there...  :p 

Note 1: there are two listeners - the Dot Net (System.Diagnostics.Trace.Listeners) and the Logging Block's