MSMQ logwriter formatter issues

Topics: Building and extending application blocks, Logging Application Block
Dec 18, 2009 at 9:15 PM
Edited Dec 18, 2009 at 9:17 PM

Hi ,I am building a MSMQ application. I am writing from an application to a queue in Text Format . And my windows service picks it up from the MSMQ and puts it in the database. 

But the problem I am facing here is that the complete string in the MSMQ is going into the message.. ITs not getting splitted up into the text fomatter pattern. I would like to know what I am doing wrong. 

Please help. 
Please find the below attached files for reference.

 Did u find the solution for this problem. I am facing the same problem . Would be glad if you can help me out.  

Thanks 

MSMQLog Distributor MSMQ to Database App.config

 

<section name="loggingConfiguration"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" />
		<section name="dataConfiguration"
				 type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null " />
		<section name="msmqDistributorSettings"
				 type="XXX.Logging.LogService.Configuration.MsmqDistributorSettings, ConsumerService_LoggingService" />
	</configSections>
	<loggingConfiguration name="Logging Application Block"
						  tracingEnabled="true"
						  defaultCategory="General"
						  logWarningsWhenNoCategoriesMatch="true">
		<listeners>
			<add databaseInstanceName="LogConnection"
				 writeLogStoredProcName="WriteLog"
				 addCategoryStoredProcName="AddCategory"
				 formatter="Text Formatter"
				 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
				 traceOutputOptions="None"
				 filter="All"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
				 name="Database Trace Listener" />
		</listeners>
		<formatters>
			<add template="Timestamp:  {timestamp}&#xD;&#xA;Message:  {message}&#xD;&#xA;Category:  {category}&#xD;&#xA;Priority:  {priority}&#xD;&#xA;EventId:  {eventid}&#xD;&#xA;Severity:  {severity}&#xD;&#xA;Title:  {title}&#xD;&#xA;Brand:  {brand}&#xD;&#xA;Farm:  {farm}&#xD;&#xA;Machine:  {machine}&#xD;&#xA;Application Domain:  {appDomain}&#xD;&#xA;Process Id:  {processId}&#xD;&#xA;Process Name:  {processName}&#xD;&#xA;Win32 Thread Id:  {win32ThreadId}&#xD;&#xA;Thread Name:  {threadName}&#xD;&#xA;Extended Properties:  {dictionary({key} - {value}&#xD;&#xA;)}"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
				 name="Text Formatter" />
		</formatters>
		<categorySources>
			<add switchValue="All"
				 name="General">
				<listeners>
					<add name="Database Trace Listener" />
				</listeners>
			</add>
			<add switchValue="All"
				 name="Information">
				<listeners>
					<add name="Database Trace Listener" />
				</listeners>
			</add>
			<add switchValue="All"
				 name="Error">
				<listeners>
					<add name="Database Trace Listener" />
				</listeners>
			</add>
		</categorySources>
		<specialSources>
			<allEvents switchValue="All"
					   name="All Events" />
			<notProcessed switchValue="All"
						  name="Unprocessed Category" />
			<errors switchValue="All"
					name="Logging Errors Warnings">
				<listeners>
					<add name="Database Trace Listener" />
				</listeners>
			</errors>
		</specialSources>
	</loggingConfiguration>
RECEIVED QUEUE MESSAGE METHOD in MSMQ log DISTRIBUTOR - MSMQ to Database Service 
    private void ReceiveQueuedMessages()
        {
            this.isCompleted = false;
            while (!IsQueueEmpty())
            {
                using (MessageQueue msmq = CreateMessageQueue())
                {
                    System.Messaging.Message message = msmq.Peek();

                    string serializedEntry = message.Body.ToString();
                    LogEntry logEntry = new LogEntry();
                    try
                    {
                        logEntry.Message = serializedEntry;
                    }
                    catch (FormatException formatException)
                    {
                        string logMessage = string.Format(
                            Resources.Culture,
                            Resources.ExceptionCouldNotDeserializeMessageFromQueue,
                            message.Id,
                            msmq.Path);

                        this.eventLogger.LogServiceFailure(
                            logMessage,
                            formatException,
                            TraceEventType.Error);

                        throw new LoggingException(logMessage, formatException);
                    }
                    catch (SerializationException serializationException)
                    {
                        string logMessage = string.Format(
                            Resources.Culture,
                            Resources.ExceptionCouldNotDeserializeMessageFromQueue,
                            message.Id,
                            msmq.Path);

                        this.eventLogger.LogServiceFailure(
                            logMessage,
                            serializationException,
                            TraceEventType.Error);

                        throw new LoggingException(logMessage, serializationException);
                    }

                    if (logEntry != null)
                    {
                        msmq.Formatter = new ActiveXMessageFormatter();
                        // Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(logEntry);
                        logWriter.Write(logEntry);

                        //logEntry.Title = "Consumer Service Log";
                        // logWriter.Write(logEntry);
                    }

                    message = msmq.Receive();

                    if (this.StopReceiving)
                    {
                        this.isCompleted = true;
                        return;
                    }
                }
            }
Application that writes to MSMQ App.config

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<section name="loggingConfiguration"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
		<section name="dataConfiguration"
				 type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
	</configSections>
	<loggingConfiguration name="Logging Application Block"
						  tracingEnabled="true"
						  defaultCategory="General"
						  logWarningsWhenNoCategoriesMatch="true">
		<listeners>
			<add name="Formatted EventLog TraceListener"
				 source="Enterprise Library Logging"
				 formatter="Text Formatter"
				 log="Application"
				 machineName=""
				 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
				 traceOutputOptions="None"
				 filter="All"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
         />
			<add name="Msmq TraceListener"
				 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
				 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.MsmqTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
				 traceOutputOptions="None"
				 filter="All"
				 queuePath="Formatname:DIRECT=OS:XXX\private$\XXX"
				 formatter="Text Formatter"
				 messagePriority="Normal"
				 timeToReachQueue="49710.06:28:15"
				 timeToBeReceived="49710.06:28:15"
				 recoverable="false"
				 useAuthentication="false"
				 useDeadLetterQueue="false"
				 useEncryption="false"
				 transactionType="None" />
		</listeners>


	   <formatters>
      <add template="Timestamp:  {timestamp}&#xD;&#xA;Message:  {message}&#xD;&#xA;Category:  {category}&#xD;&#xA;Priority:  {priority}&#xD;&#xA;EventId:  {eventid}&#xD;&#xA;Severity:  {severity}&#xD;&#xA;Title:  {title}&#xD;&#xA;Brand:  {brand}&#xD;&#xA;Farm:  {farm}&#xD;&#xA;Machine:  {machine}&#xD;&#xA;Application Domain:  {appDomain}&#xD;&#xA;Process Id:  {processId}&#xD;&#xA;Process Name:  {processName}&#xD;&#xA;Win32 Thread Id:  {win32ThreadId}&#xD;&#xA;Thread Name:  {threadName}&#xD;&#xA;Extended Properties:  {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
        name="Text Formatter" />
    </formatters>
		<categorySources>
			<add switchValue="All"
				 name="Error">
				<listeners>
					<add name="Msmq TraceListener" />
				</listeners>
			</add>
			<add switchValue="All"
				 name="General">
				<listeners>
					<add name="Msmq TraceListener" />
				</listeners>
			</add>
			<add switchValue="All"
				 name="Information">
				<listeners>
					<add name="Msmq TraceListener" />
				</listeners>
			</add>
		</categorySources>
		<specialSources>
			<allEvents switchValue="All"
					   name="All Events" />
			<notProcessed switchValue="All"
						  name="Unprocessed Category" />
			<errors switchValue="All"
					name="Logging Errors &amp; Warnings">
				<listeners>
					<add name="Formatted EventLog TraceListener" />
					<add name="Msmq TraceListener" />
				</listeners>
			</errors>
		</specialSources>
	</loggingConfiguration>
</configuration>

 

Code that writes to MSMQ 
  Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry logEntry = new LogEntry();
            logEntry.EventId = 100;
            logEntry.Priority = 2;
            logEntry.Message = "DISTRIBUTOR TEST";
            logEntry.Brand = "Thrift";
            logEntry.Farm = "ZTZRBrands";

            DTG.Global.Object.Message message = new DTG.Global.Object.Message(5, "error trying to make a res");

 Logger.Write(logEntry);

 

 

Please help. 

 

Thanks 

Anjana 

 

Dec 21, 2009 at 2:25 AM

Your MSMQ Tracelistener should use the Binary Formatter since you used it with the Distributor Service.  Modify your config first and let me know if it solves your problem.

 

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