Ejemplo n.º 1
0
        public void IntegrationLogWithRollForLengthTest()
        {
            var files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("rollTest*.Log");

            foreach (var file in files)
            {
                file.Delete();
            }
            var appender = new RollingFileAppender("rollTest.Log");

            LogManager.AppenderMediator.Subscribe(appender);
            var logger = LogManager.GetLogger(typeof(RollingFileAppenderTests));

            logger.Info("Appender is logging");
            appender.Dispose();
            appender.MaxLength = 0;
            logger.Info("Appender is logging in a rolling file");
            appender.Dispose();
            files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("rollTest*.Log");
            Assert.Equal(2, files.Length);

            LogManager.AppenderMediator.Unsubscribe(appender);
            appender           = new RollingFileAppender("rollTest.Log");
            appender.MaxLength = 0;
            LogManager.AppenderMediator.Subscribe(appender);
            logger.Info("Appender is logging again");
            appender.Dispose();
            files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("rollTest*.Log");
            Assert.Equal(3, files.Length);
        }
Ejemplo n.º 2
0
        public void IntegrationLogInSubDirectoryTest()
        {
            var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory() + "/Logs");

            if (directoryInfo.Exists)
            {
                var files = directoryInfo.GetFiles("myTest*.Log");
                foreach (var file in files)
                {
                    file.Delete();
                }
            }
            var appender = new RollingFileAppender("Logs/myTest.Log");

            LogManager.AppenderMediator.Subscribe(appender);
            var logger = LogManager.GetLogger(typeof(RollingFileAppenderTests));

            logger.Info("Appender is logging");
            appender.Dispose();
            LogManager.AppenderMediator.Unsubscribe(appender);
            appender = new RollingFileAppender("Logs/myTest.Log");
            LogManager.AppenderMediator.Subscribe(appender);
            logger.Info("Appender is logging again");
            appender.Dispose();
            var logFiles = directoryInfo.GetFiles("myTest*.Log");

            Assert.Equal(2, logFiles.Length);
        }
Ejemplo n.º 3
0
        public void LogOnLockedFileTest()
        {
            var files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("otherTest*.Log");

            foreach (var file in files)
            {
                file.Delete();
            }
            var appender = new RollingFileAppender("otherTest.Log");

            LogManager.AppenderMediator.Subscribe(appender);
            var logger = LogManager.GetLogger(typeof(RollingFileAppenderTests));

            logger.Info("Appender is logging");
            LogManager.AppenderMediator.Unsubscribe(appender);
            appender.Dispose();
            files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("otherTest*.Log");
            Assert.Equal(1, files.Length);
            var fileLog = files.FirstOrDefault();

            // grab a lock on the log file
            using (File.Open(fileLog.FullName, FileMode.Open, FileAccess.Write, FileShare.None))
            {
                appender = new RollingFileAppender("otherTest.Log");
                LogManager.AppenderMediator.Subscribe(appender);
                logger.Info("Appender is logging again");
                appender.Dispose();
                files = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles("otherTest*.Log");
                Assert.Equal(2, files.Length);
            }
        }
        private void Append(RollingFileAppender appender)
        {
            var task = appender.AppendAsync(BuildLogEvent());

            task.Wait();
            CheckLog();
            _appender.Dispose();
        }