Enterprise Library Logging Block and MSMQ Part 2 MSMQ Distributor Configuration

The Enterprise Library comes with a service called MSMQ Distributor which can be used to poll messages from a queue and write them to a database in a transactional and safe way.

This means that if the database is down, if the service polls a message and tries to write it to the DB, it will fail. the message will stay in the queue and wont be lost. When the database is back, the messages will be written in the database in the order they arrived in the queue without loosing any bit. Another advantage is when a lot of messages are sent at the same time, the queue will work as a buffer preventing the database engine to receive too many requests at the same time.

Install MSMQ service.

From Manage Server, install the Msmq Feature.

Install Enterprise Library on the DB server (or copy the dll and exe needed)

By default the binaries will be in the following Folder : C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin

Create a new transactional private Queue.

MSMQ Queue Creation

Ensure the group Everyone has the following accesses:

  • Peek Message
  • Get Properties
  • Get Permissions
  • Send Message

MSMQ Queue Creation Security

Configure the MSMQ Service and Start it

Configure the Msmq Distributor Service

Edit the MsmqDistributor.exe.config file and get to the msmqDistributorSettings tag :

1
2
3
4
<msmqDistributorSettings 
    msmqPath=".\Private$\q_log" 
    queueTimerInterval="1000"  
    serviceName="Enterprise Library Distributor Service" />

msmqpath : Name of the queue defined above.

queueTimerInterval : Interval in milliseconds between each poll on the queue.

serviceName : Name shown in the service manager page.

Add a Connection String Section

1
2
3
4
    <connectionStrings>
        <add name="SQL_Logging" connectionString="Database=Logging;Server=ServerName;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

Add a Database Listener in the LoggingConfiguration section:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
	<loggingConfiguration tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    ...
    		<listeners>
            <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                databaseInstanceName="SQL_Logging" writeLogStoredProcName="WriteLog"
                addCategoryStoredProcName="All" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
		</listeners>

    ...
	</loggingConfiguration>

Then, install the Msmq Distributor Service as a service.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe “C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin\MsmqDistributor.exe”