Exemplo n.º 1
0
        public static void Main()
        {
            IFormatter formatter = new SampleFormatter();
            FileAppender appender = new FileAppender("file.txt", formatter);

            Logger logger = new Logger(appender);

            int a = 10;
            try
            {
                logger.Critical("a cannot be 10");
                logger.Warn("a cannot be even");
            }
            finally
            {
                appender.Close();
            }
        }
        public static void Main()
        {
            var simpleFormatter = new SimpleFormatter();
            var xmlFormatter = new XmlFormatter();
            // Create own formatter
            var jsonFormatter = new JsonFormatter();

            var fileAppender = new FileAppender(jsonFormatter, "log.txt");
            var consoleAppender = new ConsoleAppender(xmlFormatter);
            // Create own appender
            var sqlAppender = new SqlAppender(jsonFormatter);

            Logger logger = new Logger(consoleAppender, fileAppender);

            logger.Critical("Out of memory");
            logger.Info("Unused local variable 'name'");

            fileAppender.Close();
        }
Exemplo n.º 3
0
        static void Main()
        {
            // Introduce custom formatter
            IFormatter formatter = new CrazyFormatter();

            FileAppender appender = new FileAppender("file.txt", formatter);
            Logger logger = new Logger(appender);

            try
            {
                logger.Critical("a cannot be 5");
                logger.Critical("Out of memory");
                // CauseException();
                logger.Info("Unused local variable 'name'");
            }
            finally
            {
                appender.Close();
            }
        }
Exemplo n.º 4
0
        public void TestFileAppender_ShouldAppendOnFile()
        {
            const string FakeResult = "It works";
            const string FilePath = "../../Log.txt";
            string expetedOutput = "It works" + Environment.NewLine;
            DateTime date = It.IsAny<DateTime>();
            const ReportLevel ReportLevel = ReportLevel.Error;
            var message = It.IsAny<string>();
            var mock = new Mock<ILayout>();
            mock.Setup(layout => layout.LayoutMaker(date, ReportLevel, message)).Returns(FakeResult);
            var fileAppender = new FileAppender(mock.Object, FilePath);

            Assert.IsTrue(File.Exists(FilePath));
            fileAppender.Append(date, ReportLevel, message);
            fileAppender.Close();
            StreamReader reader = new StreamReader(FilePath);
            string actualOutput = reader.ReadToEnd();
            reader.Close();
            mock.Verify(a => a.LayoutMaker(date, ReportLevel, message), Times.Once);
            Assert.AreEqual(expetedOutput, actualOutput);
        }