Example #1
0
 protected override void ProcessLogQueue(ConcurrentQueue<LogEvent> logQueue, LogEventDispatcher dispatcher)
 {
     // Open a buffered stream writer to the console standard out
     LogEvent logEvent;
     using (var writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
     {
         // Pull all of the log events from the queue and write them to the buffered writer
         while (logQueue.IsEmpty == false)
         {
             if (logQueue.TryDequeue(out logEvent))
             {
                 try
                 {
                     writer.Write(Layout.FormatLogEvent(logEvent));
                 }
                 catch (Exception e)
                 {
                     try
                     {
                         writer.Flush();
                     }
                     finally
                     {
                         if (dispatcher != null)
                             dispatcher.HandleException(e, logEvent);
                         else
                             throw e;
                     }
                 }
             }
         }
         writer.Flush();
     }
 }
Example #2
0
		/// <summary>
		/// Processes the log queue of events.  This is used for asynchronous logging.  The worker thread calls this function
		/// to handle queued log events.  The dispatcher is provided for access to the other parts of the framework that
		/// may be needed for logging purposes, for example, the tag keeper.
		///
		/// Override this function to provide advanced asynchronous logging logic.  By default, this function simply calls
		/// <see cref="Log" />.  Additional performance gains can be had, especially when using an external resource such
		/// as a file, database or service, by opening the handle/connection once and using it for each of the log events,
		/// as opposed to opening and closing the handle/connection for each log event.  Special consideration should be
		/// taken for "playing nice", if you expect that processing the log events in this queue may be expensive in terms
		/// of CPU and cycles, consider processing a limited number of log events from the queue before breaking, releasing
		/// the control back to the system more courteously.
		///
		/// TODO: Consider removing the connection of the dispatcher in favor of the TagKeeper, reducing the coupling needed
		///   and complexity.
		/// </summary>
		/// <param name="logQueue">The queue of log events to log</param>
		/// <param name="dispatcher">The dispatcher that owns this target</param>
		protected virtual void ProcessLogQueue(ConcurrentQueue<LogEvent> logQueue, LogEventDispatcher dispatcher)
		{
			// Iterate over the queue, removing and logging each log event
			LogEvent logEvent;
			while (logQueue.IsEmpty == false)
			{
				if (logQueue.TryDequeue(out logEvent))
				{
					try
					{
						Log(logEvent);
					}
					catch (Exception e)
					{
						if (dispatcher != null)
							dispatcher.HandleException(e, logEvent);
						else
							throw e;
					}
				}
			}
		}
Example #3
0
		protected override void ProcessLogQueue(ConcurrentQueue<LogEvent> logQueue, LogEventDispatcher dispatcher)
		{
			LogEvent logEvent;
			lock (_fileLock)
				using (var fileStream = new StreamWriter(new BufferedStream(File.Open(Config.FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))))
					while (logQueue.IsEmpty == false && !QuickRollCheck())
					{
						if (logQueue.TryDequeue(out logEvent))
						{
							try
							{
								fileStream.Write(Layout.FormatLogEvent(logEvent));
								_lastWrite = GetDateTime();
							}
							catch (Exception e)
							{
								if (dispatcher != null)
									dispatcher.HandleException(e, logEvent);
								else
									throw e;
							}
						}
					}

			if (QuickRollCheck())
				RollFile();

			_sw.Restart();
		}