示例#1
0
        public void CustomHeaderAndFooter()
        {
            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "server1",
                AddNewLines = true,
                Layout      = "${message}",
                Header      = "First event: ${logger}",
                Footer      = "Last event: ${logger}",
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Single(mmt.CreatedMocks);

            var mock = mmt.CreatedMocks[0];

            Assert.Single(mock.MessagesSent);
            var    msg          = mock.MessagesSent[0];
            string expectedBody = "First event: MyLogger1\nlog message 1\nlog message 2\nlog message 3\nLast event: MyLogger3\n";

            Assert.Equal(expectedBody, msg.Body);
        }
示例#2
0
        public void ErrorHandlingTest()
        {
            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "${logger}",
                Body        = "${message}",
                AddNewLines = true,
            };

            var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
            {
                cfg.Configuration.AddRuleForAllLevels(mmt);
            }).LogFactory;

            try
            {
                LogManager.ThrowExceptions = false;

                var exceptions  = new List <Exception>();
                var exceptions2 = new List <Exception>();

                mmt.WriteAsyncLogEvents(
                    new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                    new LogEventInfo(LogLevel.Debug, "ERROR", "log message 2").WithContinuation(exceptions2.Add),
                    new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
                Assert.Null(exceptions[0]);
                Assert.Null(exceptions[1]);

                Assert.NotNull(exceptions2[0]);
                Assert.Equal("Some SMTP error.", exceptions2[0].Message);
            }
            finally
            {
                LogManager.ThrowExceptions = true;
            }

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.Equal(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];

            Assert.Equal("MyLogger1", mock1.Host);
            Assert.Single(mock1.MessagesSent);

            var msg1 = mock1.MessagesSent[0];

            Assert.Equal("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];

            Assert.Equal("ERROR", mock2.Host);
            Assert.Single(mock2.MessagesSent);

            var msg2 = mock2.MessagesSent[0];

            Assert.Equal("log message 2\n", msg2.Body);
        }
示例#3
0
        public void ErrorHandlingTest()
        {
            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "${logger}",
                Body        = "${message}",
                AddNewLines = true,
            };

            mmt.Initialize(null);

            var exceptions  = new List <Exception>();
            var exceptions2 = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "ERROR", "log message 2").WithContinuation(exceptions2.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
            Assert.IsNull(exceptions[0], Convert.ToString(exceptions[0]));
            Assert.IsNull(exceptions[1], Convert.ToString(exceptions[1]));

            Assert.IsNotNull(exceptions2[0]);
            Assert.AreEqual("Some SMTP error.", exceptions2[0].Message);

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.AreEqual(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];

            Assert.AreEqual("MyLogger1", mock1.Host);
            Assert.AreEqual(1, mock1.MessagesSent.Count);

            var msg1 = mock1.MessagesSent[0];

            Assert.AreEqual("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];

            Assert.AreEqual("ERROR", mock2.Host);
            Assert.AreEqual(1, mock2.MessagesSent.Count);

            var msg2 = mock2.MessagesSent[0];

            Assert.AreEqual("log message 2\n", msg2.Body);
        }
示例#4
0
        public void CsvLayoutTest()
        {
            var layout = new CsvLayout()
            {
                Delimiter  = CsvColumnDelimiterMode.Semicolon,
                WithHeader = true,
                Columns    =
                {
                    new CsvColumn("name",    "${logger}"),
                    new CsvColumn("level",   "${level}"),
                    new CsvColumn("message", "${message}"),
                }
            };

            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "server1",
                AddNewLines = true,
                Layout      = layout,
            };

            var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
            {
                cfg.Configuration.AddRuleForAllLevels(mmt);
            }).LogFactory;

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Single(mmt.CreatedMocks);

            var mock = mmt.CreatedMocks[0];

            Assert.Single(mock.MessagesSent);
            var    msg          = mock.MessagesSent[0];
            string expectedBody = "name;level;message\nMyLogger1;Info;log message 1\nMyLogger2;Debug;log message 2\nMyLogger3;Error;log message 3\n";

            Assert.Equal(expectedBody, msg.Body);
        }
示例#5
0
        public void CsvLayoutTest()
        {
            var layout = new CsvLayout()
            {
                Delimiter  = CsvColumnDelimiterMode.Semicolon,
                WithHeader = true,
                Columns    =
                {
                    new CsvColumn("name",    "${logger}"),
                    new CsvColumn("level",   "${level}"),
                    new CsvColumn("message", "${message}"),
                }
            };

            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "server1",
                AddNewLines = true,
                Layout      = layout,
            };

            layout.Initialize(null);

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.IsNull(exceptions[0], Convert.ToString(exceptions[0]));

            Assert.AreEqual(1, mmt.CreatedMocks.Count);

            var mock = mmt.CreatedMocks[0];

            Assert.AreEqual(1, mock.MessagesSent.Count);
            var    msg          = mock.MessagesSent[0];
            string expectedBody = "name;level;message\nMyLogger1;Info;log message 1\nMyLogger2;Debug;log message 2\nMyLogger3;Error;log message 3\n";

            Assert.AreEqual(expectedBody, msg.Body);
        }
示例#6
0
        public void PerMessageAddress()
        {
            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "${logger}@foo.com",
                Body        = "${message}",
                SmtpServer  = "server1.mydomain.com",
                AddNewLines = true,
            };

            var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
            {
                cfg.Configuration.AddRuleForAllLevels(mmt);
            }).LogFactory;

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.Equal(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];

            Assert.Single(mock1.MessagesSent);

            var msg1 = mock1.MessagesSent[0];

            Assert.Equal("*****@*****.**", msg1.To[0].Address);
            Assert.Equal("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];

            Assert.Single(mock2.MessagesSent);

            var msg2 = mock2.MessagesSent[0];

            Assert.Equal("*****@*****.**", msg2.To[0].Address);
            Assert.Equal("log message 2\n", msg2.Body);
        }
示例#7
0
        public void PerMessageServer()
        {
            var mmt = new MockMailTarget
            {
                From        = "*****@*****.**",
                To          = "*****@*****.**",
                SmtpServer  = "${logger}.mydomain.com",
                Body        = "${message}",
                AddNewLines = true,
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.Equal(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];

            Assert.Equal("MyLogger1.mydomain.com", mock1.Host);
            Assert.Single(mock1.MessagesSent);

            var msg1 = mock1.MessagesSent[0];

            Assert.Equal("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];

            Assert.Equal("MyLogger2.mydomain.com", mock2.Host);
            Assert.Single(mock2.MessagesSent);

            var msg2 = mock2.MessagesSent[0];

            Assert.Equal("log message 2\n", msg2.Body);
        }
示例#8
0
        public void CustomHeaderAndFooter()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "server1",
                AddNewLines = true,
                Layout = "${message}",
                Header = "First event: ${logger}",
                Footer = "Last event: ${logger}",
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Equal(1, mmt.CreatedMocks.Count);

            var mock = mmt.CreatedMocks[0];
            Assert.Equal(1, mock.MessagesSent.Count);
            var msg = mock.MessagesSent[0];
            string expectedBody = "First event: MyLogger1\nlog message 1\nlog message 2\nlog message 3\nLast event: MyLogger3\n";
            Assert.Equal(expectedBody, msg.Body);
        }
示例#9
0
        public void PerMessageAddress()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "${logger}@foo.com",
                Body = "${message}",
                SmtpServer = "server1.mydomain.com",
                AddNewLines = true,
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.Equal(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];
            Assert.Equal(1, mock1.MessagesSent.Count);

            var msg1 = mock1.MessagesSent[0];
            Assert.Equal("*****@*****.**", msg1.To[0].Address);
            Assert.Equal("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];
            Assert.Equal(1, mock2.MessagesSent.Count);

            var msg2 = mock2.MessagesSent[0];
            Assert.Equal("*****@*****.**", msg2.To[0].Address);
            Assert.Equal("log message 2\n", msg2.Body);
        }
示例#10
0
        public void CsvLayoutTest()
        {
            var layout = new CsvLayout()
            {
                Delimiter = CsvColumnDelimiterMode.Semicolon,
                WithHeader = true,
                Columns =
                {
                    new CsvColumn("name", "${logger}"),
                    new CsvColumn("level", "${level}"),
                    new CsvColumn("message", "${message}"),
                }
            };

            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "server1",
                AddNewLines = true,
                Layout = layout,
            };

            layout.Initialize(null);

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Equal(1, mmt.CreatedMocks.Count);

            var mock = mmt.CreatedMocks[0];
            Assert.Equal(1, mock.MessagesSent.Count);
            var msg = mock.MessagesSent[0];
            string expectedBody = "name;level;message\nMyLogger1;Info;log message 1\nMyLogger2;Debug;log message 2\nMyLogger3;Error;log message 3\n";
            Assert.Equal(expectedBody, msg.Body);
        }
示例#11
0
        public void ErrorHandlingTest()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "${logger}",
                Body = "${message}",
                AddNewLines = true,
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            var exceptions2 = new List<Exception>();

            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "ERROR", "log message 2").WithContinuation(exceptions2.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger1", "log message 3").WithContinuation(exceptions.Add));
            Assert.IsNull(exceptions[0], Convert.ToString(exceptions[0]));
            Assert.IsNull(exceptions[1], Convert.ToString(exceptions[1]));

            Assert.IsNotNull(exceptions2[0]);
            Assert.AreEqual("Some SMTP error.", exceptions2[0].Message);

            // 2 messages are sent, one using MyLogger1.mydomain.com, another using MyLogger2.mydomain.com
            Assert.AreEqual(2, mmt.CreatedMocks.Count);

            var mock1 = mmt.CreatedMocks[0];
            Assert.AreEqual("MyLogger1", mock1.Host);
            Assert.AreEqual(1, mock1.MessagesSent.Count);

            var msg1 = mock1.MessagesSent[0];
            Assert.AreEqual("log message 1\nlog message 3\n", msg1.Body);

            var mock2 = mmt.CreatedMocks[1];
            Assert.AreEqual("ERROR", mock2.Host);
            Assert.AreEqual(1, mock2.MessagesSent.Count);

            var msg2 = mock2.MessagesSent[0];
            Assert.AreEqual("log message 2\n", msg2.Body);
        }