MSMQ Distributor Service store the message as the xml in database

Topics: Enterprise Library Core, Exception Handling Application Block, Logging Application Block
Jun 8, 2009 at 11:43 AM

Hi All,

I am using the latest version of the Enterprise library 4.1 for implementing MSMQDistributor service. My requirement is if any exception rises , it should be stored into the MSMQ. The "Enterprise Library Distributor Service" will be running in parallel and this will read the message from MSMQ and store into the database. For these i am using the "Binary Formatter"  to log the information into the Message Queue(MSMQ) and retrieving the information from the queue to store into the database(Using the  Msmq Distributor Service). This i have implemented sucessfully.

Later I have a different requirement that, I have to store the information in xml format into the database. For this i am using the custom formatter to store the message in the MSMQ. Then I am trying to use the same custom formatter to read the information from the MSMQ to store into the db... Basically i think i need to configure the Formatter to the XmlMessageFormatter. But I am not finding any way to configure using the Enterprise Library Configuration tool. I dont know any other option to implement this. Since in my analysis the MSQMQ Distributor use only the Binary Formatter . Is ther any way i can use the XmlLogFormatter or any other option to store the logged message (in MSQMQ) and store that  information as xml in to the database

Advance thanks for the Help !!!

Here are the sample code that i used to create the custom formatter

namespace MSMQUtil.CustomFormatter
{
    public class StringWriterWithEncoding : StringWriter
    {
        Encoding _encoding;

        public StringWriterWithEncoding(Encoding encoding)
        {
            this._encoding = encoding;
        }

        public override Encoding Encoding
        {
            get
            {
                return _encoding;
            }
        }
    }

    [ConfigurationElementType(typeof(CustomFormatterData))]
    public class LongXmlFormatter : LogFormatter
    {
         private NameValueCollection Attributes = null;

        public LongXmlFormatter(NameValueCollection attributes)
        {
            this.Attributes = attributes;
        }

        public override string Format(LogEntry log)
        {
            // The read must use the same encoding when reading the XML
            using (StringWriter sw = new StringWriterWithEncoding(Encoding.UTF8))
            {
                XmlTextWriter w = new XmlTextWriter(sw);

                w.Formatting = Formatting.None;

                w.WriteStartDocument(true);
                {
                    w.WriteStartElement("LogEntry");
                    {
                        w.WriteElementString("Timestamp", log.TimeStampString);

                        w.WriteElementString("ActivityID", log.ActivityIdString);

                        w.WriteElementString("Message", log.Message);

                        w.WriteStartElement("Categories");
                        {
                            foreach (string category in log.CategoriesStrings)
                            {
                                w.WriteElementString("Category", category);
                            }
                        }
                        w.WriteEndElement();

                        w.WriteElementString("ErrorMessages", log.ErrorMessages);

                        w.WriteElementString("Priority", log.Priority.ToString());

                        w.WriteElementString("EventId", log.EventId.ToString(CultureInfo.InvariantCulture));

                     
                        w.WriteEndElement();
                    }
                    w.WriteEndElement();
                }
                w.WriteEndDocument();

                return sw.ToString();
            }
        }
    }

 

Thanks,

Siva

Jun 9, 2009 at 4:46 AM

As you already know, you can only use BinaryFormatter for the MSMQ TraceListener with the Distributor Service.  If you want to store it in XML format in the database, you can set the Formatter of your DatabaseTraceListener to use your CustomFormatter.  To make it easier for you, you can just copy the implementation of the XmlLogFormatter in the EntLib source code.  I just tried it and it works fine.

 

Sarah Urmeneta
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

 

Jun 9, 2009 at 12:11 PM
Edited Jun 9, 2009 at 12:13 PM

Sounds Great !!!  Instead of using the XmlLogFormatter code i used the CustomFormatter code which i have posted.  Thanks for the help !!