log4net.Util.CyclicBuffer.Append C# (CSharp) Méthode

Append() public méthode

Appends a loggingEvent to the buffer.

Append an event to the buffer. If the buffer still contains free space then null is returned. If the buffer is full then an event will be dropped to make space for the new event, the event dropped is returned.

public Append ( LoggingEvent loggingEvent ) : LoggingEvent
loggingEvent LoggingEvent The event to append to the buffer.
Résultat LoggingEvent
		public LoggingEvent Append(LoggingEvent loggingEvent)
		{	
			if (loggingEvent == null)
			{
				throw new ArgumentNullException("loggingEvent");
			}

			lock(this)
			{
				// save the discarded event
				LoggingEvent discardedLoggingEvent = m_events[m_last];

				// overwrite the last event position
				m_events[m_last] = loggingEvent;	
				if (++m_last == m_maxSize)
				{
					m_last = 0;
				}

				if (m_numElems < m_maxSize)
				{
					m_numElems++;
				}
				else if (++m_first == m_maxSize)
				{
					m_first = 0;
				}

				if (m_numElems < m_maxSize)
				{
					// Space remaining
					return null;
				}
				else
				{
					// Buffer is full and discarding an event
					return discardedLoggingEvent;
				}
			}
		}

Usage Example

		[Test] public void TestSize1()
		{
			CyclicBuffer cb = new CyclicBuffer(1);

			Assert.AreEqual(0, cb.Length, "Empty Buffer should have length 0");
			Assert.AreEqual(1, cb.MaxSize, "Buffer should have max size 1");

			LoggingEvent event1 = new LoggingEvent(null, null, null, null, null, null);
			LoggingEvent event2 = new LoggingEvent(null, null, null, null, null, null);

			LoggingEvent discardedEvent = cb.Append(event1);

			Assert.IsNull(discardedEvent, "No event should be discarded untill the buffer is full");
			Assert.AreEqual(1, cb.Length, "Buffer should have length 1");
			Assert.AreEqual(1, cb.MaxSize, "Buffer should still have max size 1");


			discardedEvent = cb.Append(event2);

			Assert.AreSame(event1, discardedEvent, "Expect event1 to now be discarded");
			Assert.AreEqual(1, cb.Length, "Buffer should still have length 1");
			Assert.AreEqual(1, cb.MaxSize, "Buffer should really still have max size 1");

			LoggingEvent[] discardedEvents = cb.PopAll();

			Assert.AreEqual(1, discardedEvents.Length, "Poped events length should be 1");
			Assert.AreSame(event2, discardedEvents[0], "Expect event2 to now be popped");
			Assert.AreEqual(0, cb.Length, "Buffer should be back to length 0");
			Assert.AreEqual(1, cb.MaxSize, "Buffer should really really still have max size 1");
		}
All Usage Examples Of log4net.Util.CyclicBuffer::Append