public void Send_WithCustomEvaluator_SendsAllMessages()
        {
            // Arrange
            var evaluator = new MyEvaluator();

            var appender = new MyBufferedAppender(evaluator)
            {
                SendAsync = true
            };

            appender.ActivateOptions();

            ConfigureAppender(appender);

            var logger = LogManager.GetLogger(this.GetType());

            // Act
            logger.InfoFormat("Message 1");
            logger.InfoFormat("Message 2");
            logger.InfoFormat("Message 3");

            StopApplication();

            // Assert
            Assert.That(MessageSink.Logs.Count, Is.EqualTo(3), "All remaining messages should be sent at the end");
        }
        public void Send_WithTimeEvaluator_SendsAllMessages()
        {
            /*
             * <appender name="BufferingForwardingAppender" type="buffered_appender.MyBufferedAppender">
             * <bufferSize value="3" />
             * <param name="SendAsync" value="true"/>
             * <evaluator type="log4net.Core.TimeEvaluator">
             *  <threhold value="1"/>
             * </evaluator>
             *
             * </appender>
             *
             * see http://stackoverflow.com/questions/11319319/log4net-bufferingforwardingappender-performance-issue
             */
            // Arrange
            const int oneSecond = 1;
            var       appender  = new MyBufferedAppender(new TimeEvaluator(oneSecond))
            {
                SendAsync = true
            };

            appender.ActivateOptions();

            ConfigureAppender(appender);

            var logger = LogManager.GetLogger(this.GetType());

            // Act
            logger.InfoFormat("Message 1");
            logger.InfoFormat("Message 2");
            logger.InfoFormat("Message 3");

            var messagesbeforeWait = MessageSink.Logs.ToList();

            var twoSeconds = new TimeSpan(0, 0, 0, 2);

            Thread.Sleep(twoSeconds);
            logger.InfoFormat("Message 4");

            Thread.Sleep(twoSeconds);
            var messagesAfterWait = MessageSink.Logs.ToList();

            logger.InfoFormat("Message 5");

            StopApplication();

            // Assert
            Assert.That(messagesbeforeWait.Count, Is.EqualTo(0), "No Messages should be sent before the wait time");
            Assert.That(messagesAfterWait.Count, Is.EqualTo(4), "The first four should be sent");
            Assert.That(MessageSink.Logs.Count, Is.EqualTo(5), "All remaining messages should be sent at the end");
        }
        public void Send_WithDefaultEvaluator_SendsAllMessages()
        {
            /*
             * <appender name="BufferingForwardingAppender" type="buffered_appender.MyBufferedAppender">
             * <bufferSize value="3" />
             * <param name="SendAsync" value="true"/>
             * <Fix value="0"/> <!-- Set Fix flag to NONE -->
             * </appender>
             *
             * see http://stackoverflow.com/questions/11319319/log4net-bufferingforwardingappender-performance-issue
             */

            // Arrange
            var appender = new MyBufferedAppender {
                BufferSize = 3, SendAsync = true
            };

            appender.ActivateOptions();

            ConfigureAppender(appender);

            var logger = LogManager.GetLogger(this.GetType());

            // Act
            logger.InfoFormat("Message 1");
            logger.InfoFormat("Message 2");
            logger.InfoFormat("Message 3");
            logger.InfoFormat("Message 4");
            logger.InfoFormat("Message 5");


            // Assert
            Assert.That(MessageSink.Logs.Count, Is.EqualTo(4), "The fourth message should trigger the Send event since the buffer size of 3 is full");

            StopApplication();

            Thread.Sleep(1000);
            Assert.That(MessageSink.Logs.Count, Is.EqualTo(5), "All remaining messages should be sent at the end");
        }
        public void SynchronousAppender_Send_MeasurePerformance(int bufferSize, int messageCount, AppenderType appenderType)
        {
            // Arrange
            var appender = new MyBufferedAppender
            {
                BufferSize = bufferSize,
                SendAsync  = appenderType == AppenderType.Asynchronous
            };

            appender.ActivateOptions();

            ConfigureAppender(appender);

            var logger = LogManager.GetLogger(this.GetType());

            // Act
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            for (int i = 0; i < messageCount; i++)
            {
                logger.Info(Guid.NewGuid().ToString());
            }
            stopWatch.Stop();

            StopApplication();

            // Assert
            if (MessageSink.Logs.Count != messageCount)
            {
                // Give it time to finish sending
                Thread.Sleep(2000);
            }

            Assert.That(MessageSink.Logs.Count, Is.EqualTo(messageCount), "Not all the messages were sent!");

            Console.WriteLine("Type:{3}|BufferSize:{0}|MessageCount:{1}|Time(seconds):{2}", bufferSize, messageCount, stopWatch.Elapsed.TotalSeconds, appenderType);
        }