Custom Trace Listener and TraceListenerDAta Error

Topics: Logging Application Block
Sep 1, 2010 at 4:25 PM
Edited Sep 1, 2010 at 5:24 PM

 

Hi all,
I try use Custom Trace Listener, Authenticating Email trace listener, based in © Richard Harding source code.
 

 

AuthenticatingEmailTraceListener.cs

[ConfigurationElementType(typeof(AuthenticatingEmailTraceListenerData))]
    public class AuthenticatingEmailTraceListener: Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedTraceListenerBase
    {
        string toAddress = String.Empty;
		string fromAddress = String.Empty;
		string subjectLineStarter = String.Empty;
		string subjectLineEnder = String.Empty;
		string smtpServer = String.Empty;
		int smtpPort = 25;
        bool authenticate;
        string smtpUsername = string.Empty;
        string smtpPassword = string.Empty;
        bool enableSsl;

		
		public AuthenticatingEmailTraceListener()			: base()		{		}

		public AuthenticatingEmailTraceListener(ILogFormatter formatter)
			: base(formatter)		{		}

		public AuthenticatingEmailTraceListener(string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer, ILogFormatter formatter)
			: this(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, 25,false,"","", false, formatter)
		{
		}

		public AuthenticatingEmailTraceListener(
			string toAddress,
			string fromAddress,
			string subjectLineStarter,
			string subjectLineEnder,
			string smtpServer,
			int smtpPort,
            bool authenticate,
            string smtpUsername,
            string smtpPassword,
            bool enableSsl,
			ILogFormatter formatter
		)
			: base(formatter)
		{
			this.toAddress = toAddress;
			this.fromAddress = fromAddress;
			this.subjectLineStarter = subjectLineStarter;
			this.subjectLineEnder = subjectLineEnder;
			this.smtpServer = smtpServer;
			this.smtpPort = smtpPort;
            this.authenticate = authenticate;
            this.smtpUsername = smtpUsername;
            this.smtpPassword = smtpPassword;
            this.enableSsl = enableSsl;
		}


		public AuthenticatingEmailTraceListener(string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer)
			: this(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, 25)
		{
		}


        public AuthenticatingEmailTraceListener(string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer, int smtpPort)
			: base()
		{
			this.toAddress = toAddress;
			this.fromAddress = fromAddress;
			this.subjectLineStarter = subjectLineStarter;
			this.subjectLineEnder = subjectLineEnder;
			this.smtpServer = smtpServer;
			this.smtpPort = smtpPort;
		}


        public override void Write(string message)
        {
            EmailMessage mailMessage = new EmailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, authenticate, smtpUsername, smtpPassword, enableSsl, message, this.Formatter);
            mailMessage.Send();
        }

        public override void WriteLine(string message)
        {
            Write(message);
        }


        
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry)
            {
                EmailMessage message = new EmailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, authenticate, smtpUsername, smtpPassword, enableSsl, data as LogEntry, this.Formatter);
                message.Send();
                InstrumentationProvider.FireTraceListenerEntryWrittenEvent();
            }
            else if (data is string)
            {
                Write(data);
            }
            else
            {
                base.TraceData(eventCache, source, eventType, id, data);
            }
        }
    }

AuthenticatingEmailTraceListenerData.cs

 

[Assembler(typeof(AuthenticatingEmailTraceListenerAssembler))]
    public class AuthenticatingEmailTraceListenerData: TraceListenerData
    {
        private const string toAddressProperty = "toAddress";
        private const string fromAddressProperty = "fromAddress";
        private const string subjectLineStarterProperty = "subjectLineStarter";
        private const string subjectLineEnderProperty = "subjectLineEnder";
        private const string smtpServerProperty = "smtpServer";
        private const string smtpPortProperty = "smtpPort";
        private const string formatterNameProperty = "formatter";
        private const string AuthenticateProperty = "authenticate";
        private const string smtpUsernameProperty = "username";
        private const string smtpPasswordProperty = "password";
        private const string enableSslProperty = "enableSsl";

		public AuthenticatingEmailTraceListenerData()
		{
		}

		public AuthenticatingEmailTraceListenerData(string name, string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer,bool authenticate,string smtpUsername,string smtpPassword, bool enableSsl, string formatterName)
			: this(name,toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, 25,authenticate,smtpUsername,smtpPassword, enableSsl, formatterName)
		{

		}

        public AuthenticatingEmailTraceListenerData(string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer, int smtpPort, bool authenticate, string smtpUsername, string smtpPassword, bool enableSsl, string formatterName)
            : this("unnamed", toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, authenticate, smtpUsername, smtpPassword, enableSsl, formatterName)
		{
		}

        public AuthenticatingEmailTraceListenerData(string name, string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer, int smtpPort, bool authenticate, string smtpUsername, string smtpPassword, bool enableSsl, string formatterName)
			: this(name, toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort,authenticate,smtpUsername,smtpPassword, enableSsl, formatterName, TraceOptions.None)
		{
		}

        public AuthenticatingEmailTraceListenerData(string name, string toAddress, string fromAddress, string subjectLineStarter, string subjectLineEnder, string smtpServer, int smtpPort, bool authenticate, string smtpUsername, string smtpPassword, bool enableSsl, string formatterName, TraceOptions traceOutputOptions)
            : base(name, typeof(AuthenticatingEmailTraceListener), traceOutputOptions)
		{
			this.ToAddress = toAddress;
			this.FromAddress = fromAddress;
			this.SubjectLineStarter = subjectLineStarter;
			this.SubjectLineEnder = subjectLineEnder;
			this.SmtpServer = smtpServer;
			this.SmtpPort = smtpPort;
			this.Formatter = formatterName;
            this.Authenticate = authenticate;
            this.SmtpUsername = smtpUsername;
            this.SmtpPassword = smtpPassword;
            this.EnableSsl = enableSsl;
		}
		
		[ConfigurationProperty(toAddressProperty, IsRequired = true)]
		public string ToAddress
		{
			get { return (string)base[toAddressProperty]; }
			set { base[toAddressProperty] = value; }
		}

		[ConfigurationProperty(fromAddressProperty, IsRequired = true)]
		public string FromAddress
		{
			get { return (string)base[fromAddressProperty]; }
			set { base[fromAddressProperty] = value; }
		}

// MORE CODE for properties ...

        [ConfigurationProperty(smtpPasswordProperty, IsRequired = false)]
        public string SmtpPassword
        {
            get { return (string)base[smtpPasswordProperty]; }
            set { base[smtpPasswordProperty] = value; }
        }

        [ConfigurationProperty(enableSslProperty, IsRequired = false)]
        public bool EnableSsl
        {
            get { return (bool)base[enableSslProperty]; }
            set { base[enableSslProperty] = value; }
        }
        

    }

AuthenticatingEmailTraceListenerAssembler.cs

public class AuthenticatingEmailTraceListenerAssembler: Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerAsssembler
    {
        public override System.Diagnostics.TraceListener Assemble(IBuilderContext context, Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            AuthenticatingEmailTraceListenerData castedObjectConfiguration
                = (AuthenticatingEmailTraceListenerData)objectConfiguration;

            ILogFormatter formatter = GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache);

            System.Diagnostics.TraceListener createdObject
                = new AuthenticatingEmailTraceListener(                    
                    castedObjectConfiguration.ToAddress,
                    castedObjectConfiguration.FromAddress,
                    castedObjectConfiguration.SubjectLineStarter,
                    castedObjectConfiguration.SubjectLineEnder,
                    castedObjectConfiguration.SmtpServer,
                    castedObjectConfiguration.SmtpPort,
                    castedObjectConfiguration.Authenticate,
                    castedObjectConfiguration.SmtpUsername,
                    castedObjectConfiguration.SmtpPassword,
                    castedObjectConfiguration.EnableSsl,
                    formatter);

            return createdObject;
        }
    }

Listener in my app.config
 <add name="MoverOperacionesErroresFicherosAuthEmailTraceListener"
              type="GrupoBackup.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListener, GrupoBackup.Frk.Logging.AuthEmailTraceListener"
              listenerDataType="GrupoBackup.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListenerData, GrupoBackup.Frk.Logging.AuthEmailTraceListener"
              formatter="Exception Formatter"
              traceOutputOptions="None"
              toAddress="xxxxxxxxxxxx@gmail.com"
              fromAddress="xxxxxxxxxxxx@gmail.com"
              subjectLineStarter="OGF Expediente Electronico. Excepci�n detectada - "
              subjectLineEnder="IncidenciasOGF"
              smtpServer="smtp.gmail.com"
              smtpPort="587"
              authenticate="true"
              username="xxxxxxxx@gmail.com"
              password="xxxxxxxx"
              enableSsl="true"
           />

 

 

Now, I use 

  listenerDataType="GrupoBackup.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListenerData, GrupoBackup.Frk.Logging.AuthEmailTraceListener"
        
and get unknown error    

but If I use 

 
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 
then I get error when I send email because parameters are Empty (like toAddress, fromAddress, etc).

Exception Type: System.Argument
ExceptionMessage: 'address' cannot be Empty.
ParamName: address

Any suggestions, please?
Sep 2, 2010 at 10:55 AM

Unfortunately, I was unable to install your full integration from my end (might have missed something here).

  Can you send (entlib.support@avanade.com) us a copy of your project of this implementation to help us for further investigation? For now, I'll try what I can get from your post to see if I can be able to dig down thru the cause of the problem. I'll just post you the update.  

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Sep 2, 2010 at 7:51 PM

I send you the project that I use. Thanks.

Sep 3, 2010 at 2:31 AM

I'm currently looking into this.  I'm seeing a StackOverFlowException logged in the event viewer.  I'll update you once I find out more about this.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 3, 2010 at 6:43 AM

The cause of the error when adding the Authenticating Email TraceListener is the EnableSsl property of the AuthenticatingEmailTraceListenerNode class.  The original code was :

[SRDescription("EmailTraceListenerEnableSslDescription", typeof(Resources))]
[SRCategory("CategoryGeneral", typeof(Resources))]
public bool EnableSsl
{
        get { return EnableSsl; }
        set { EnableSsl = value; }
}

modify it to use the enableSsl variable instead of EnableSsl which causes the StackOverflowException.

[SRDescription("EmailTraceListenerEnableSslDescription", typeof(Resources))]
[SRCategory("CategoryGeneral", typeof(Resources))]
public bool EnableSsl
{
        get { return enableSsl; }
        set { enableSsl = value; }
}

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com