Logging Application Block - When Finished?

Topics: Logging Application Block
Feb 14, 2011 at 5: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;

            DatabaseHelper.ClearLogs(); 

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

            {

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

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

               MEL.Logger.Write(logEntryCopy); 

           }

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

Feb 15, 2011 at 7:10 AM

Hi,

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.
entlib.support@avanade.com

Feb 15, 2011 at 8: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 11:48 PM

Yes, LAB runs synchronously.

 

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