Logging Block with MSMQ

Topics: Exception Handling Application Block, Logging Application Block
Feb 15, 2012 at 8:37 PM

I have specific questions on the Enterprise library logging block.

  1. I am looking to send in exception from remote clients using MSMQ to a collector box that in turn writes to a Database. Is this pattern built in to the block ?
  2. Also when you initiate a log request does it spawn a new thread or does it wait for it to "send" blocking the running thread ?
  3. What kind of reporting is provided for logged errors ?
  4. Is it possible to setup alerts on an exception ?

 

 

Thanks!

Feb 17, 2012 at 3:49 PM

> I am looking to send in exception from remote clients using MSMQ to a collector box that in turn writes to a Database. Is this pattern built in to the block ?

Yes, you can use the message queuing trace listener along with the Distributor Service.  

> Also when you initiate a log request does it spawn a new thread or does it wait for it to "send" blocking the running thread ?

No, logging is done synchronously.  You could start a new thread yourself if you want to log asynchronously: 

ThreadPool.QueueUserWorkItem((o) => Logger.Write("Test", "General"));

> What kind of reporting is provided for logged errors ?

Can you expand on what you mean by reporting?  Do you mean out of the box reports based on your log files/database?  If so, then Enterprise Library does not provide reporting features.

> Is it possible to setup alerts on an exception ?

Do you mean monitoring of log files/database and triggering notifications to systems/personnel (e.g. Tivoli)?  If so, then Enterprise Library does not provide this out of the box.  You could write some sort of alerting trace listener to provide alerts when messages are logged (similar to the email trace listener).

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Feb 17, 2012 at 4:24 PM

Thanks Randy for your reply. I do have a couple more questions:

Is it possible to extract just the logging block from EL 5.0 ? I want it to make it as lightweight as possible.

Can the database schema it writes to be customized ?

Feb 17, 2012 at 5:43 PM

You can just use (and reference and deploy) the logging block without the other blocks.  There are still dependencies on Unity, however.  So, if you were just using the logging block you would still need to reference:

Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.ServiceLocation
Microsoft.Practices.Unity.Configuration
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Interception

The database trace listener uses a stored procedure to write to the database so it is quite simple to modify the stored procedure to support any schema as long as the interface does not change.  The stored procedure interface is:

CREATE PROCEDURE [dbo].[WriteLog]
(
	@EventID int, 
	@Priority int, 
	@Severity nvarchar(32), 
	@Title nvarchar(256), 
	@Timestamp datetime,
	@MachineName nvarchar(32), 
	@AppDomainName nvarchar(512),
	@ProcessID nvarchar(256),
	@ProcessName nvarchar(512),
	@ThreadName nvarchar(512),
	@Win32ThreadId nvarchar(128),
	@Message nvarchar(1500),
	@FormattedMessage ntext,
	@LogId int OUTPUT
)

If you need to support additional data to log then the easiest way is to add that data to the LogEntry's ExtendedProperties and use a custom trace listener to write that information to the database (see the Extensibility Hands on Labs for a lab on how to develop one).  You could also look at using the ExtendedPropertyDatabaseTraceListener from the Sample Projects which is a general purpose solution that writes all extended properties to the database as key/value pairs in a separate table.   


 --
Randy Levy
Enterprise Library support engineer
entlib.support@live.com