asynchronous logging using LAB

Topics: Building and extending application blocks, Logging Application Block
Nov 24, 2008 at 12:42 AM
Hi,
   I am wondering if I can extend the logging application block to perform the logging aynchronously without using MSMQ trace listener? I have written a helper class with static methods in it. where I tried to call the write method of logger class in a separate thread.  My problem is that when I tried to call this method from a MS test, I get the ThreadAbort  error with a message 'Thread was being aborted.' It seems to me that somehow my logging thread is getting aborted when main thread is ending.  Requirement is to encaspsulate the threading part in the logging library, so that the client  DLLcan continue to execute the logic for main operation. Is there a  simple way to achieve this or I have to encapsulate the logic in a separate web service? Below is the code I tried to extend the logging block.

Class that will do the work in seperate thread
 internal class LoggerThread
 {
  LogEntry logEntry;
  WindowsIdentity impersonateIdentity;
  public LoggerThread(LogEntry logEntry, WindowsIdentity impersonateIdentity)
  {
   this.logEntry = logEntry;
   this.impersonateIdentity = impersonateIdentity;
  }
  public void LogMessage()
  {
   WindowsImpersonationContext ctx = null;
   try
   {
    ctx = impersonateIdentity.Impersonate();
    Logger.Write(this.logEntry);
   }
   catch (ThreadAbortException taException)
   {
    //if we get this exception, We can not do anything here
    System.Diagnostics.EventLog.WriteEntry("PVsLogging", "Logging thread is aborted unexpectedly. " + taException.Message, EventLogEntryType.Error, 9002);
   }
   catch (Exception ex)
   {
    Logger.Write(ex.Message, "PvsLogger", 1, 9001);
   }
   finally
   {
    // Revert impersonation
    if (ctx != null)
     ctx.Undo();
   }

  }
 }


Class that will be called by Client DLL

public class PvsLogger
 {
 public static void LogMesaageAsync(LogEntry logEntry)
  {
   WindowsIdentity  impersonateIdentity = WindowsIdentity.GetCurrent();
   LoggerThread loggerThread = new LoggerThread(logEntry, impersonateIdentity);
   Thread aThread = new Thread(new ThreadStart(loggerThread.LogMessage));
   aThread.Start();

  }
 }
}

Thanks.
Amit

Nov 24, 2008 at 1:08 AM
Hi, check out this link first, http://systematically.blogspot.com/2008/01/asynchronous-logging-using-logging.html.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 24, 2008 at 3:43 AM
Hi Sarah,
  Thanks for the link, but when I used the code in the link, I get the same error. "Thread was being aborted." I think the main problem is that my client assembly that runs the parent thread is finishing execution before the LoggerThread finishes its work. Somehow, I need to make sure in the client assembly that the child logger thread gets completed before it finishes execution of main thread.  Since overall time taken by the logger.write call is in miliseconds, I am not even sure that if it is worth doing that. I will be having a better idea once I do the load test.
Thanks
Amit