コード例 #1
0
        public void SimpleTest()
        {
            container.Register(Component.For(typeof(SimpleLoggingComponent)).Named("component1"));
            SimpleLoggingComponent test = container.Resolve <SimpleLoggingComponent>("component1");

            test.DoSomething();

            String expectedLogOutput = String.Format("|INFO|{0}|Hello world", typeof(SimpleLoggingComponent).FullName);
            String actualLogOutput   = (NLog.LogManager.Configuration.FindTargetByName("memory") as MemoryTarget).Logs[0].ToString();

            actualLogOutput = actualLogOutput.Substring(actualLogOutput.IndexOf('|'));

            Assert.AreEqual(expectedLogOutput, actualLogOutput);

            container.Register(Component.For(typeof(SmtpServer)).Named("component2"));
            ISmtpServer smtpServer = container.Resolve <ISmtpServer>("component2");

            smtpServer.Start();
            smtpServer.InternalSend("*****@*****.**", "*****@*****.**", "We're looking for a few good porgrammars.");
            smtpServer.Stop();

            expectedLogOutput = String.Format("|INFO|Castle.Facilities.Logging.Tests.Classes.SmtpServer|InternalSend [email protected] [email protected] We're looking for a few good porgrammars.", typeof(SmtpServer).FullName);
            actualLogOutput   = (NLog.LogManager.Configuration.FindTargetByName("memory") as MemoryTarget).Logs[1].ToString();
            actualLogOutput   = actualLogOutput.Substring(actualLogOutput.IndexOf('|'));

            Assert.AreEqual(expectedLogOutput, actualLogOutput.ToString());
        }
コード例 #2
0
        public void SimpleTest()
        {
            container.Register(Component.For(typeof(SimpleLoggingComponent)).Named("component1"));
            SimpleLoggingComponent test = container.Resolve <SimpleLoggingComponent>("component1");

            test.DoSomething();

            String         expectedLogOutput = String.Format("[INFO ] [{0}] - Hello world" + Environment.NewLine, typeof(SimpleLoggingComponent).FullName);
            MemoryAppender memoryAppender    = ((Hierarchy)LogManager.GetRepository()).Root.GetAppender("memory") as MemoryAppender;
            TextWriter     actualLogOutput   = new StringWriter();
            PatternLayout  patternLayout     = new PatternLayout("[%-5level] [%logger] - %message%newline");

            patternLayout.Format(actualLogOutput, memoryAppender.GetEvents()[0]);

            Assert.AreEqual(expectedLogOutput, actualLogOutput.ToString());

            container.Register(Component.For(typeof(SmtpServer)).Named("component2"));
            ISmtpServer smtpServer = container.Resolve <ISmtpServer>("component2");

            smtpServer.Start();
            smtpServer.InternalSend("*****@*****.**", "*****@*****.**", "We're looking for a few good porgrammars.");
            smtpServer.Stop();

            expectedLogOutput = String.Format("[DEBUG] [Castle.Facilities.Logging.Tests.Classes.SmtpServer] - Stopped" + Environment.NewLine, typeof(SimpleLoggingComponent).FullName);
            memoryAppender    = ((Hierarchy)LogManager.GetRepository()).Root.GetAppender("memory") as MemoryAppender;
            actualLogOutput   = new StringWriter();
            patternLayout     = new PatternLayout("[%-5level] [%logger] - %message%newline");

            Assert.AreEqual(memoryAppender.GetEvents().Length, 4);

            patternLayout.Format(actualLogOutput, memoryAppender.GetEvents()[3]);

            Assert.AreEqual(expectedLogOutput, actualLogOutput.ToString());
        }