コード例 #1
0
        public void FileAppenderTest()
        {
            var file = Executable.Relative("Logs", "nunit-log.txt");

            Directory.CreateDirectory(Executable.Relative("Logs"));
            File.Delete(file);
            while (File.Exists(file))
            {
                Thread.Yield();
            }

            //order matters, dispose runner then appender
            using (var appender = new WriterLogAppender(file))
                using (var runner = new LogRunner(new PatternLogFormatter("{LEVEL} {NAME} {MESSAGE}")))
                {
                    runner.AddAppender(appender);

                    var logger = new Logger(runner, "N$AME");

                    logger.Debug("Message {0}", 1);
                    logger.Info("Message {0}", 2);
                    logger.Warn("Message {0}", 3);
                    logger.Error("Message {0}", 4);
                    logger.Success("Message {0}", 5);
                }

            var lines = File.ReadAllLines(file);

            //no DEBUG to file
            Assert.AreEqual(4, lines.Length);
            Assert.AreEqual("INFO N$AME Message 2", lines[0]);
            Assert.AreEqual("WARN N$AME Message 3", lines[1]);
            Assert.AreEqual("ERROR N$AME Message 4", lines[2]);
            Assert.AreEqual("SUCCESS N$AME Message 5", lines[3]);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: samuelventura/SharpTools
        private static void RunLogger(string[] args)
        {
            using (var runner = new LogRunner(new PatternLogFormatter("{LEVEL} {NAME} {MESSAGE}")))
            {
                runner.AddAppender(new ConsoleLogAppender());

                var logger = new Logger(runner, "N$AME");

                logger.Debug("Message {0}", 1);
                logger.Info("Message {0}", 2);
                logger.Warn("Message {0}", 3);
                logger.Error("Message {0}", 4);
                logger.Success("Message {0}", 5);
            }
        }
コード例 #3
0
        public void ThreadTimestampVerbosityTest()
        {
            var appender = new StringAppender();

            using (var runner = new LogRunner(new PatternLogFormatter("{THREAD} {VERBOSITY} {TIMESTAMP:yyyy-MM-dd HH:mm:ss.fff} {MESSAGE}")))
            {
                runner.AddAppender(appender);

                var logger = new Logger(runner, "nunit", () => { return(DateTime.Parse("2019-01-02 23:24:25.876")); });

                logger.Debug("Message {0}", 1);
                logger.Debug(1, "Message {0}", 2);
            }

            Assert.AreEqual(2, appender.Logs.Count);
            Assert.AreEqual(Thread.CurrentThread.Name + " 0 2019-01-02 23:24:25.876 Message 1", appender.Logs[0].Line);
            Assert.AreEqual(Thread.CurrentThread.Name + " 1 2019-01-02 23:24:25.876 Message 2", appender.Logs[1].Line);
        }
コード例 #4
0
        public void AppenderExceptionTest()
        {
            var appender = new ExceptionAppender();

            using (var runner = new LogRunner(new PatternLogFormatter("{LEVEL} {NAME} {MESSAGE}")))
            {
                runner.AddAppender(appender);

                var logger = new Logger(runner, "N$AME");

                logger.Debug("Message {0}", 1);
                logger.Info("Message {0}", 2);
                logger.Warn("Message {0}", 3);
                logger.Error("Message {0}", 4);
                logger.Success("Message {0}", 5);
            }

            Assert.AreEqual(2, appender.Logs.Count);
            Assert.AreEqual("DEBUG N$AME Message 1", appender.Logs[0].Line);
            Assert.AreEqual("INFO N$AME Message 2", appender.Logs[1].Line);
            //discarded from WARN on
        }
コード例 #5
0
        public void BasicTest()
        {
            var appender = new StringAppender();

            using (var runner = new LogRunner(new PatternLogFormatter("{LEVEL} {NAME} {MESSAGE}")))
            {
                runner.AddAppender(appender);

                var logger = new Logger(runner, "N$AME");

                logger.Debug("Message {0}", 1);
                logger.Info("Message {0}", 2);
                logger.Warn("Message {0}", 3);
                logger.Error("Message {0}", 4);
                logger.Success("Message {0}", 5);
            }

            Assert.AreEqual(5, appender.Logs.Count);
            Assert.AreEqual("DEBUG N$AME Message 1", appender.Logs[0].Line);
            Assert.AreEqual("INFO N$AME Message 2", appender.Logs[1].Line);
            Assert.AreEqual("WARN N$AME Message 3", appender.Logs[2].Line);
            Assert.AreEqual("ERROR N$AME Message 4", appender.Logs[3].Line);
            Assert.AreEqual("SUCCESS N$AME Message 5", appender.Logs[4].Line);
        }