Include UserName in formatter

Topics: Logging Application Block
Dec 16, 2010 at 12:40 PM
Edited Dec 16, 2010 at 12:41 PM

I use Ent Lib 4.1, vs 2008, win 2003 server, and win xp in development:

I use loggingconfiguration, and a custom .ZZZZHtmlEmailTraceListener, that uses a TextFormatter

How can I include username value in formatter ??

I have: 

template="Machine: {machine}. Mensaje: {message}"

Thanks

Class (partial code)

 [ConfigurationElementType(typeof(CustomTraceListenerData))]
    //[DebuggerNonUserCode()]
    public class ZZZZHtmlEmailTraceListener : CustomTraceListener
    {

        string toAddress = String.Empty;
        ....

        private bool initialized;

        public void Initialize()
        {
            if (initialized)
                return;
            this.toAddress = this.Attributes["toAddress"];
....
            initialized = true;
        }

       ...

        public ZZZZHtmlEmailTraceListener(): base()
        {
            instrumentationProvider = new LoggingInstrumentationProvider();
            initialized = false;
        }


        public override void Write(string message)
        {
            HtmlMailMessage mailMessage = new HtmlMailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, message, base.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)
            {
                Initialize();
                CheckValidParams();
                HtmlMailMessage mailMessage = new HtmlMailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, data as LogEntry, base.Formatter);
                mailMessage.Send();
                InstrumentationProvider.FireTraceListenerEntryWrittenEvent();
            }
            else if (data is string)
            {
                Write(data);
            }
            else
            {
                base.TraceData(eventCache, source, eventType, id, data);
            }
        }

        /// <summary>
        /// Declare the supported attributes for <see cref="EmailTraceListener"/>
        /// </summary>
        protected override string[] GetSupportedAttributes()
        {
            return new string[7] { "formatter", "toAddress", "fromAddress", "subjectLineStarter", "subjectLineEnder", "smtpServer", "smtpPort" };
        }

       ...

    }

 

 

Listener

 

 

<add toAddress="xxxx@zzzz.es;aaaa@zzzz.es"
 fromAddress="noreply@zzzz.es"
subjectLineStarter="Portal Mediadores Excepcion"
 smtpServer="des.zzz.es"smtpPort="25" formatter="Email Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=00000"
 traceOutputOptions="DateTime" filter="All" type="ZZZ.Frk.Logging.TraceListeners.ZZZZHtmlEmailTraceListener, ZZZ.Frk.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=00000"
name="Email TraceListener" />
Formatter

 

 

<add name="Email Formatter" template="Machine: {machine}. Mensaje: {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=00000" />

 

 

Dec 17, 2010 at 4:50 AM

One approach is to add it in your LogEntry object ExtendedProperties collection property. Just make sure the token "{dictionary({key} - {value}{newline})}" is added in the template of your formatter. Sample code:

 

 

            LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            LogEntry log = new LogEntry();
            log.Categories.Add("General");
            log.Message = "Test Log";
            log.ExtendedProperties.Add("Username", "MyUserName");
            writer.Write(log);

 

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

Dec 20, 2010 at 3:47 AM

Gino's suggestion doesn't require you any code changes.  As long as you add the username in the ExtendedProperties in your logentry object, it will be included in what gets written in the destination as long as you include the token {dictionary({key}-{value}{newline})} in your TextFormatter.

Another approach, if you don't like to add the entire content of the ExtendedProperties would be to use the keyValue token.  If you used the code above, you can add the token

{keyValue(Username)}

in your formatter so that it will be replaced with the value "MyUserName".

 

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