Logging Application Block - When Finished?

Topics: Logging Application Block
Feb 14, 2011 at 4:48 PM

I am trying to implement a logging solution using Microsoft's Enterprise Library (MEL).  I have written a custom trace listener to write customized data to a database.  I wrote a unit test that writes 5000 logs using the Logger.LogWrite() method.  I have my custom listener configured.  After the logging code is run, I call Trace.Flush() and then call to the database to check how many rows have been entered.  90% of the time the row count matches 5000; however, sometimes it is under 100 rows.  

My question is how can I be sure that the LogWriter is finished writing before I call my assert statement to check the row count?  I was hoping the Flush() method would have taken care of that.  So, is there a way of knowing that all my logs are done writing?


My unit test:

using MEL = Microsoft.Practices.EnterpriseLibrary.Logging;

            //set how many to write

           int logCount = 5000;


            for (int index = 0; index < logCount; index++)


              MEL.LogEntry logEntryCopy = logEntry.Clone() as MEL.LogEntry;

               logEntryCopy.Message = string.Format("Single thread, index: {0}", index); 



            Assert.AreEqual(logCount, DatabaseHelper.CountLogs(), "Not all logs were written");

Feb 15, 2011 at 6:10 AM


I was unable to reproduce the problem using something like your posted code above. I don't think the LogWriter also has any property that can be used as synch lock that can be used to check if logging is still in progress. With this, I'm thinking if implementing a simple locking implementation would help just to make sure the logging is completed before the next line of code is executed. Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

Feb 15, 2011 at 7:39 PM

I had a problem elsewhere in my code that was causing me problems.

By digging into the enterprise code I learned that when you call the log write, it executes on a single thread, so when the method returns it is done processing.

Feb 15, 2011 at 10:48 PM

Yes, LAB runs synchronously.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.