Reading an Event Log From Message Que And Writing To DB

Topics: Logging Application Block
Jun 18, 2010 at 8:08 AM

I am using Enterprise Library to log the exceptions to MessageQueue using BinaryMessageFormatter.

Now using a Windows Service I should read the Messages in the message queue and write them to DB.

I would like to know how to convert the Message in the MSMQ into EventLog format.

Jun 18, 2010 at 8:18 AM

 Let me ask first if you really need the functionality or reading messages from the message queue to be in that windows service.  Coz if not, you can opt to use EntLib's Distributor Service.  It's specifically designed for reading messages from a queue and writing them to database, event viewer, and those other which is supported by EntLib's trace listeners.

 

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

Jun 18, 2010 at 8:51 AM

Thanks for replying Sarah.

Yup this is a specific requirement to read it from message queue into eventlog format, process them, and write into specific DB of specific structure.

- Kannan Balasubramanian

Jun 18, 2010 at 9:05 AM

Are you going to use EntLib's trace listener to log to the database and to the event viewer?

I'm not sure what you meant by "eventlog format"; EntLib's Logging Application Block uses a LogEntry object and is able to log its contents to a database and event viewer.

Anyway, looking at EntLib's source code, specifically in the Logging.MsmqDistributor's MsmqLogDistributor will help.  Or you can google how to read message from message queue.

 

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

Jun 18, 2010 at 9:10 AM

I want the MSMQ message to be de-serailized to LogEntry format and read them.

Writing to DB would be done using a custom code and not trace listener.

Jun 18, 2010 at 9:23 AM

You can use EntLib's BinaryLogFormatter class.

MessageQueue message = //read message from queue;
string serializedEntry = message.Body.ToString();
LogEntry logEntry = BinaryLogFormatter.Deserialize(serializedEntry)

This is a code snippet from the ReceiveQueueMessages method of the MsmqLogDistributor class.  You can check that out.

 

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

Jun 18, 2010 at 9:48 AM

Yup I did the same thing after going through the soure code. I have a small issue.

In the LogEntry I have information like "Source","TargetSite" etc. But once I read and use the "BinaryLogFormatter.Deserialize" these information are not there either in LogEntry nor in ExtendedProperties.

Jun 18, 2010 at 10:44 AM

I found the Source, TargetSite etc found as part of LogEntry as a string. Guess I have to parse them and then use them.

Thanks for the support Sarah.