public void TestLevelEvaluator()
        {
            m_bufferingForwardingAppender.Evaluator = new LevelEvaluator(Level.Info);
            m_bufferingForwardingAppender.ActivateOptions();
            log4net.Config.BasicConfigurator.Configure(m_hierarchy, m_bufferingForwardingAppender);

            ILogger logger = m_hierarchy.GetLogger("TestLevelEvaluator");

            logger.Log(typeof(EvaluatorTest), Level.Debug, "Debug message logged", null);
            logger.Log(typeof(EvaluatorTest), Level.Debug, "Debug message logged", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 2 events buffered");

            logger.Log(typeof(EvaluatorTest), Level.Info, "Info message logged", null);
            Assert.AreEqual(3, m_countingAppender.Counter, "Test 3 events flushed on Info message.");
        }
Esempio n. 2
0
        /// <summary>
        /// Creates Buffer Appender, Responsible for storing all logging events in memory buffer
        /// and writing them down when by passing the logging events on to the file appender it holds
        /// when its buffer becomes full
        /// Can also be made a lossy logger which writes only writes down to a file when a specific crieteria/condition is met
        /// </summary>
        /// <param name="cacheName">CacheName used to name the Buffer Appender</param>
        /// <param name="fileName">File name to log into</param>
        /// <returns>Returns the created Appender</returns>
        private log4net.Appender.IAppender CreateBufferAppender(string fileName)
        {
            log4net.Appender.BufferingForwardingAppender appender = new BufferingForwardingAppender();
            appender.Name = "BufferingForwardingAppender" + _loggerName;
            //Pick from config
            int bufferSize = bufferDefaultSize;

            ReadConfig(out bufferSize);
            if (bufferSize == bufferDefaultSize)
            {
                ReadClientConfig(out bufferSize);
            }

            if (bufferSize < 1)
            {
                bufferSize = bufferDefaultSize;
            }

            appender.BufferSize = bufferSize;

            //Threshold is maintained by the logger rather than the appenders
            appender.Threshold = log4net.Core.Level.All;

            //Adds the appender to which it will pass on all the logging levels upon filling up the buffer
            appender.AddAppender(CreateRollingFileAppender(fileName));

            //necessary to apply the appender property changes
            appender.ActivateOptions();

            return(appender);
        }
        /// <summary>
        /// </summary>
        [Test] public void TestBufferSize5()
        {
            SetupRepository();

            m_bufferingForwardingAppender.BufferSize = 5;
            m_bufferingForwardingAppender.ActivateOptions();

            Assert.AreEqual(m_countingAppender.Counter, 0);

            ILogger logger = m_hierarchy.GetLogger("test");

            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 1", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 1 event in buffer");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 2", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 2 event in buffer");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 3", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 3 event in buffer");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 4", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 4 event in buffer");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 5", null);
            Assert.AreEqual(0, m_countingAppender.Counter, "Test 5 event in buffer");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 6", null);
            Assert.AreEqual(6, m_countingAppender.Counter, "Test 0 event in buffer. 6 event sent");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 7", null);
            Assert.AreEqual(6, m_countingAppender.Counter, "Test 1 event in buffer. 6 event sent");
            logger.Log(typeof(BufferingAppenderTest), Level.Warn, "Message 8", null);
            Assert.AreEqual(6, m_countingAppender.Counter, "Test 2 event in buffer. 6 event sent");
        }
        private void SetupRepository()
        {
            m_hierarchy = new Repository.Hierarchy.Hierarchy();

            m_countingAppender = new CountingAppender();
            m_countingAppender.ActivateOptions();

            m_bufferingForwardingAppender = new BufferingForwardingAppender();
            m_bufferingForwardingAppender.AddAppender(m_countingAppender);

            m_bufferingForwardingAppender.BufferSize = 0;
            m_bufferingForwardingAppender.ClearFilters();
            m_bufferingForwardingAppender.Evaluator      = null;
            m_bufferingForwardingAppender.Fix            = FixFlags.Partial;
            m_bufferingForwardingAppender.Lossy          = false;
            m_bufferingForwardingAppender.LossyEvaluator = null;
            m_bufferingForwardingAppender.Threshold      = Level.All;

            m_bufferingForwardingAppender.ActivateOptions();

            log4net.Config.BasicConfigurator.Configure(m_hierarchy, m_bufferingForwardingAppender);
        }
Esempio n. 5
0
        public static void Setup(string path)
        {
            // "%d[%2%t] %-5p[%-10c] %m %n"
            var pl = new PatternLayout {
                ConversionPattern = "%c%m%n"
            };

            pl.ActivateOptions();

            var fileAppender = new FileAppender
            {
                AppendToFile = true,
                LockingModel = new FileAppender.MinimalLock(),
                File         = path,
                Layout       = pl
            };

            fileAppender.ActivateOptions();

            var bufferingForwardingAppender = new BufferingForwardingAppender
            {
                Lossy = false,
                Fix   = FixFlags.None
            };

            bufferingForwardingAppender.AddAppender(fileAppender);
            bufferingForwardingAppender.ActivateOptions();

            var consoleAppender = new ConsoleAppender {
                Layout = pl
            };

            consoleAppender.ActivateOptions();

            var hierarchy = (Hierarchy)LogManager.GetRepository();

            hierarchy.Root.RemoveAllAppenders();
            BasicConfigurator.Configure(hierarchy, bufferingForwardingAppender, consoleAppender);
        }
Esempio n. 6
0
        private static IAppender CreateFileAppender(FileInfo loggerFilePath)
        {
            var rollingFileAppender = new RollingFileAppender
            {
                Name = "RollFileAppender",
                File = loggerFilePath.FullName,
                StaticLogFileName  = true,
                AppendToFile       = false,
                LockingModel       = new FileAppender.ExclusiveLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                MaxSizeRollBackups = 10,
                MaximumFileSize    = "10MB",
            };

            var layout = new PatternLayout
            {
                ConversionPattern = "%date{yyyy-MM-dd} %date{hh:mm:ss.ff}  [%level]  %message%newline"
            };

            rollingFileAppender.Layout = layout;
            rollingFileAppender.ActivateOptions();
            layout.ActivateOptions();

            var bufappender = new BufferingForwardingAppender
            {
                Name       = "BuffAppender",
                BufferSize = 512,
                Fix        = 0,
                Lossy      = false,
                Evaluator  = new LevelEvaluator(Level.Error)
            };

            bufappender.ActivateOptions();
            bufappender.AddAppender(rollingFileAppender);

            return(bufappender);
        }