/// <summary> /// Writes from the internal queue to the appropriate /// log handlers. The loghandler should be defined in the /// logger configuration file. The handler will be selected /// based on the source specified in the logevent object. /// </summary> private void WriteToHandler() { lock (m_qEvents.SyncRoot) //maybe locking _monitor would be good enough? in that case a generic Queue<LogEvent> could be used :) { try { if (m_qEvents.Count > 0) { Debug.WriteLine("Entries found in queue, writing to handler.", "Logger.WriteToHandler"); HandlerFactory.SetCurrentAppName(m_sAppSourceName); while (m_qEvents.Count > 0) { if (m_qEvents.Peek() != null) //debug: this check might not be needed, but added as a safety when using multithreading. { LogEvent oEvent = (LogEvent)m_qEvents.Dequeue(); //TODO: should probably add the event to the queue again if an exception occurs in the code below! string sHandlerName = GetHandlerName(oEvent.Source, oEvent.Level); if (sHandlerName.Length > 0) { HandlerFactory.GetHandler(sHandlerName).Log(oEvent); } } else { m_qEvents.Dequeue(); //debug: experienced null values in the queue during multithread stress test... } } Debug.WriteLine("Done writing to handler.", "Logger.WriteToHandler"); } else { Debug.WriteLine("No entries found in queue.", "Logger.WriteToHandler"); } } catch (Exception exp) { //For now we call ExceptionWriter, this will stop the timer and null the queue Debug.WriteLine("Exception in Logger.WriteToHandler: " + exp.Message, "Logger.WriteToHandler"); ExceptionWriter("An exception occured in Logger.WriteToHandler: " + exp.Message, exp); } finally { HandlerFactory.DisposeAllHandlers(); } } }