Example #1
0
        [Test] public void TestSize2()
        {
            CyclicBuffer cb = new CyclicBuffer(2);

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

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

            LoggingEvent discardedEvent = null;

            discardedEvent = cb.Append(event1);
            Assert.IsNull(discardedEvent, "No event should be discarded after append 1");
            discardedEvent = cb.Append(event2);
            Assert.IsNull(discardedEvent, "No event should be discarded after append 2");

            discardedEvent = cb.Append(event3);
            Assert.AreSame(event1, discardedEvent, "Expect event1 to now be discarded");

            discardedEvent = cb.PopOldest();
            Assert.AreSame(event2, discardedEvent, "Expect event2 to now be discarded");

            LoggingEvent[] discardedEvents = cb.PopAll();

            Assert.AreEqual(1, discardedEvents.Length, "Poped events length should be 1");
            Assert.AreSame(event3, discardedEvents[0], "Expect event3 to now be popped");
            Assert.AreEqual(0, cb.Length, "Buffer should be back to length 0");
            Assert.AreEqual(2, cb.MaxSize, "Buffer should really really still have max size 2");
        }
        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");
        }
        public void TestSize1()
        {
            CyclicBuffer cb = new CyclicBuffer(1);

            Assert.AreEqual(0, cb.Length, "空缓冲区的长度应该为0");   //Empty Buffer should have length 0
            Assert.AreEqual(1, cb.MaxSize, "缓冲区的最大大小应该是1"); //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, "缓冲区的长度应该为1");        //Buffer should have length 1
            Assert.AreEqual(1, cb.MaxSize, "缓冲区的大小应该仍然是1");     //Buffer should still have max size 1


            discardedEvent = cb.Append(event2);

            Assert.AreSame(event1, discardedEvent, "现在可以预期event1将被丢弃"); //Expect event1 to now be discarded
            Assert.AreEqual(1, cb.Length, "缓冲区的长度应该仍然为1");              //Buffer should still have length 1
            Assert.AreEqual(1, cb.MaxSize, "缓冲区的大小应该仍然是1");             //Buffer should really still have max size 1

            LoggingEvent[] discardedEvents = cb.PopAll();

            Assert.AreEqual(1, discardedEvents.Length, "取出事件的长度应该是1");    //Poped events length should be 1
            Assert.AreSame(event2, discardedEvents[0], "预计event2现在会被取出"); //Expect event2 to now be popped
            Assert.AreEqual(0, cb.Length, "缓冲区的长度应该回到0");                 //Buffer should be back to length 0
            Assert.AreEqual(1, cb.MaxSize, "缓冲区的大小应该仍然是1");               //Buffer should really really still have max size 1
        }
        /// <summary>
        /// This method is called by the <see cref="M:AppenderSkeleton.DoAppend(LoggingEvent)" /> method.
        /// </summary>
        /// <param name="loggingEvent">the event to log</param>
        /// <remarks>
        /// <para>
        /// Stores the <paramref name="loggingEvent" /> in the cyclic buffer.
        /// </para>
        /// <para>
        /// The buffer will be sent (i.e. passed to the <see cref="M:log4net.Appender.BufferingAppenderSkeleton.SendBuffer(log4net.Core.LoggingEvent[])" />
        /// method) if one of the following conditions is met:
        /// </para>
        /// <list type="bullet">
        ///     <item>
        ///         <description>The cyclic buffer is full and this appender is
        ///         marked as not lossy (see <see cref="P:log4net.Appender.BufferingAppenderSkeleton.Lossy" />)</description>
        ///     </item>
        ///     <item>
        ///         <description>An <see cref="P:log4net.Appender.BufferingAppenderSkeleton.Evaluator" /> is set and
        ///         it is triggered for the <paramref name="loggingEvent" />
        ///         specified.</description>
        ///     </item>
        /// </list>
        /// <para>
        /// Before the event is stored in the buffer it is fixed
        /// (see <see cref="M:LoggingEvent.FixVolatileData(FixFlags)" />) to ensure that
        /// any data referenced by the event will be valid when the buffer
        /// is processed.
        /// </para>
        /// </remarks>
        protected override void Append(LoggingEvent loggingEvent)
        {
            if (m_cb == null || m_bufferSize <= 1)
            {
                if (!m_lossy || (m_evaluator != null && m_evaluator.IsTriggeringEvent(loggingEvent)) || (m_lossyEvaluator != null && m_lossyEvaluator.IsTriggeringEvent(loggingEvent)))
                {
                    if (m_eventMustBeFixed)
                    {
                        loggingEvent.Fix = Fix;
                    }
                    SendBuffer(new LoggingEvent[1]
                    {
                        loggingEvent
                    });
                }
                return;
            }
            loggingEvent.Fix = Fix;
            LoggingEvent loggingEvent2 = m_cb.Append(loggingEvent);

            if (loggingEvent2 != null)
            {
                if (!m_lossy)
                {
                    SendFromBuffer(loggingEvent2, m_cb);
                    return;
                }
                if (m_lossyEvaluator == null || !m_lossyEvaluator.IsTriggeringEvent(loggingEvent2))
                {
                    loggingEvent2 = null;
                }
                if (m_evaluator != null && m_evaluator.IsTriggeringEvent(loggingEvent))
                {
                    SendFromBuffer(loggingEvent2, m_cb);
                }
                else if (loggingEvent2 != null)
                {
                    SendBuffer(new LoggingEvent[1]
                    {
                        loggingEvent2
                    });
                }
            }
            else if (m_evaluator != null && m_evaluator.IsTriggeringEvent(loggingEvent))
            {
                SendFromBuffer(null, m_cb);
            }
        }