Ejemplo n.º 1
0
        public void MailTarget_WithoutSubject_SendsMessageWithDefaultSubject()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}"
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

            Assert.Null(exceptions[0]);
            Assert.Single(mmt.CreatedMocks);
            var mock = mmt.CreatedMocks[0];

            Assert.Single(mock.MessagesSent);

            Assert.Equal($"Message from NLog on {Environment.MachineName}", mock.MessagesSent[0].Subject);
        }
Ejemplo n.º 2
0
        public void MailTarget_UseSystemNetMailSettings_True_KeepOverridenFiles()
        {
            var configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var section       = configuration.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;

            section.From = null;

            var mmt = new MockMailTarget()
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}",
                UseSystemNetMailSettings = true,
                MailSettings             = section
            };

            mmt.Initialize(null);
            mmt.ConfigureMailClient();

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Debug, "MyLogger", "log message").WithContinuation(exceptions.Add));

            Assert.Equal("'*****@*****.**'", mmt.From.ToString());
        }
Ejemplo n.º 3
0
        public void NoReplaceNewlinesWithBreakInHtmlMail()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                Subject    = "Hello from NLog",
                SmtpServer = "server1",
                Body       = "${level}${newline}${logger}${newline}${message}",
                Html       = true,
                ReplaceNewlineWithBrTagInHtml = false
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

            var messageSent = mmt.CreatedMocks[0].MessagesSent[0];

            Assert.True(messageSent.IsBodyHtml);
            var lines = messageSent.Body.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            Assert.True(lines.Length == 3);
        }
Ejemplo n.º 4
0
        public void MailTarget_WithNewlineInSubject_SendsMail()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                CC         = "[email protected];[email protected]",
                Bcc        = "[email protected];[email protected]",
                Subject    = "Hello from NLog\n",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}"
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").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];
        }
Ejemplo n.º 5
0
        public void MailTarget_WithEmptyTo_ThrowsNLogRuntimeException()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "",
                Subject    = "Hello from NLog",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}",
            };

            mmt.Initialize(null);
            var exceptions = new List <Exception>();

            Assert.Throws <NLogRuntimeException>(() => mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add)));
        }
Ejemplo n.º 6
0
        public void SimpleEmailTest()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                CC         = "[email protected];[email protected]",
                Bcc        = "[email protected];[email protected]",
                Subject    = "Hello from NLog",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}"
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Single(mmt.CreatedMocks);

            var mock = mmt.CreatedMocks[0];

            Assert.Single(mock.MessagesSent);
            Assert.Equal("server1", mock.Host);
            Assert.Equal(27, mock.Port);
            Assert.False(mock.EnableSsl);
            Assert.Null(mock.Credentials);

            var msg = mock.MessagesSent[0];

            Assert.Equal("Hello from NLog", msg.Subject);
            Assert.Equal("*****@*****.**", msg.From.Address);
            Assert.Single(msg.To);
            Assert.Equal("*****@*****.**", msg.To[0].Address);
            Assert.Equal(2, msg.CC.Count);
            Assert.Equal("*****@*****.**", msg.CC[0].Address);
            Assert.Equal("*****@*****.**", msg.CC[1].Address);
            Assert.Equal(2, msg.Bcc.Count);
            Assert.Equal("*****@*****.**", msg.Bcc[0].Address);
            Assert.Equal("*****@*****.**", msg.Bcc[1].Address);
            Assert.Equal("Info MyLogger log message 1", msg.Body);
        }
Ejemplo n.º 7
0
        public void MailTarget_WithoutPriority_SendsMailWithNormalPriority()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                Subject    = "Hello from NLog",
                SmtpServer = "server1",
            };

            mmt.Initialize(null);

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(_ => { }));

            var messageSent = mmt.CreatedMocks[0].MessagesSent[0];

            Assert.Equal(MailPriority.Normal, messageSent.Priority);
        }
Ejemplo n.º 8
0
        public void SimpleEmailTest()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                CC = "[email protected];[email protected]",
                Bcc = "[email protected];[email protected]",
                Subject = "Hello from NLog",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}"
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

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

            var mock = mmt.CreatedMocks[0];
            Assert.Equal(1, mock.MessagesSent.Count);
            Assert.Equal("server1", mock.Host);
            Assert.Equal(27, mock.Port);
            Assert.False(mock.EnableSsl);
            Assert.Null(mock.Credentials);

            var msg = mock.MessagesSent[0];
            Assert.Equal("Hello from NLog", msg.Subject);
            Assert.Equal("*****@*****.**", msg.From.Address);
            Assert.Equal(1, msg.To.Count);
            Assert.Equal("*****@*****.**", msg.To[0].Address);
            Assert.Equal(2, msg.CC.Count);
            Assert.Equal("*****@*****.**", msg.CC[0].Address);
            Assert.Equal("*****@*****.**", msg.CC[1].Address);
            Assert.Equal(2, msg.Bcc.Count);
            Assert.Equal("*****@*****.**", msg.Bcc[0].Address);
            Assert.Equal("*****@*****.**", msg.Bcc[1].Address);
            Assert.Equal(msg.Body, "Info MyLogger log message 1");
        }
Ejemplo n.º 9
0
        public void MailTarget_WithValidToAndEmptyBcc_SendsMail()
        {
            var mmt = new MockMailTarget
            {
                From       = "*****@*****.**",
                To         = "*****@*****.**",
                Bcc        = "",
                Subject    = "Hello from NLog",
                SmtpServer = "server1",
                SmtpPort   = 27,
                Body       = "${level} ${logger} ${message}",
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
            Assert.Single(mmt.CreatedMocks);
            Assert.Single(mmt.CreatedMocks[0].MessagesSent);
        }
Ejemplo n.º 10
0
        public void NtlmEmailTest()
        {
            var mmt = new MockMailTarget
            {
                From               = "*****@*****.**",
                To                 = "*****@*****.**",
                SmtpServer         = "server1",
                SmtpAuthentication = SmtpAuthenticationMode.Ntlm,
            };

            mmt.Initialize(null);

            var exceptions = new List <Exception>();

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Single(mmt.CreatedMocks);

            var mock = mmt.CreatedMocks[0];

            Assert.Equal(CredentialCache.DefaultNetworkCredentials, mock.Credentials);
        }
Ejemplo n.º 11
0
        public void BasicAuthEmailTest()
        {
            try
            {
                var mmt = new MockMailTarget
                {
                    From               = "*****@*****.**",
                    To                 = "*****@*****.**",
                    SmtpServer         = "server1",
                    SmtpAuthentication = SmtpAuthenticationMode.Basic,
                    SmtpUserName       = "******",
                    SmtpPassword       = "******",
                };

                mmt.Initialize(null);

                var exceptions = new List <Exception>();
                MappedDiagnosticsContext.Set("username", "u1");
                MappedDiagnosticsContext.Set("password", "p1");
                mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
                Assert.Null(exceptions[0]);

                Assert.Single(mmt.CreatedMocks);

                var mock       = mmt.CreatedMocks[0];
                var credential = mock.Credentials as NetworkCredential;
                Assert.NotNull(credential);
                Assert.Equal("u1", credential.UserName);
                Assert.Equal("p1", credential.Password);
                Assert.Equal(string.Empty, credential.Domain);
            }
            finally
            {
                MappedDiagnosticsContext.Clear();
            }
        }
Ejemplo n.º 12
0
        public void NoReplaceNewlinesWithBreakInHtmlMail()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                Subject = "Hello from NLog",
                SmtpServer = "server1",
                Body = "${level}${newline}${logger}${newline}${message}",
                Html = true,
                ReplaceNewlineWithBrTagInHtml = false
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

            var messageSent = mmt.CreatedMocks[0].MessagesSent[0];
            Assert.True(messageSent.IsBodyHtml);
            var lines = messageSent.Body.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            Assert.True(lines.Length == 3);
        }
Ejemplo n.º 13
0
        public void NtlmEmailTest()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "server1",
                SmtpAuthentication = SmtpAuthenticationMode.Ntlm,
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

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

            var mock = mmt.CreatedMocks[0];
            Assert.Equal(CredentialCache.DefaultNetworkCredentials, mock.Credentials);
        }
Ejemplo n.º 14
0
        public void MailTarget_WithoutSubject_SendsMessageWithDefaultSubject()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}"
            };
            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

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

            Assert.Equal(string.Format("Message from NLog on {0}", Environment.MachineName), mock.MessagesSent[0].Subject);
        }
Ejemplo n.º 15
0
        public void MailTarget_WithEmptySmtpServer_ThrowsNLogRuntimeException()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                Subject = "Hello from NLog",
                SmtpServer = "",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}"
            };
            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

            Assert.NotNull(exceptions[0]);
            Assert.IsType<NLogRuntimeException>(exceptions[0]);
        }
Ejemplo n.º 16
0
        public void MailTarget_WithValidToAndEmptyBcc_SendsMail()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                Bcc = "",
                Subject = "Hello from NLog",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}",
            };
            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));

            Assert.Null(exceptions[0]);
            Assert.Equal(1, mmt.CreatedMocks.Count);
            Assert.Equal(1, mmt.CreatedMocks[0].MessagesSent.Count);
        }
Ejemplo n.º 17
0
        public void MailTarget_WithInvalidPriority_SendsMailWithNormalPriority()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                Subject = "Hello from NLog",
                SmtpServer = "server1",
                Priority = "invalidPriority"
            };
            mmt.Initialize(null);

            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(_ => { }));

            var messageSent = mmt.CreatedMocks[0].MessagesSent[0];
            Assert.Equal(MailPriority.Normal, messageSent.Priority);
        }
Ejemplo n.º 18
0
        public void MailTarget_WithNewlineInSubject_SendsMail()
        {
            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                CC = "[email protected];[email protected]",
                Bcc = "[email protected];[email protected]",
                Subject = "Hello from NLog\n",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}"
            };

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").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];
        }
Ejemplo n.º 19
0
        public void BasicAuthEmailTest()
        {
            try
            {
                var mmt = new MockMailTarget
                {
                    From = "*****@*****.**",
                    To = "*****@*****.**",
                    SmtpServer = "server1",
                    SmtpAuthentication = SmtpAuthenticationMode.Basic,
                    SmtpUserName = "******",
                    SmtpPassword = "******",
                };

                mmt.Initialize(null);

                var exceptions = new List<Exception>();
                MappedDiagnosticsContext.Set("username", "u1");
                MappedDiagnosticsContext.Set("password", "p1");
                mmt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "log message 1").WithContinuation(exceptions.Add));
                Assert.Null(exceptions[0]);

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

                var mock = mmt.CreatedMocks[0];
                var credential = mock.Credentials as NetworkCredential;
                Assert.NotNull(credential);
                Assert.Equal("u1", credential.UserName);
                Assert.Equal("p1", credential.Password);
                Assert.Equal(string.Empty, credential.Domain);
            }
            finally
            {
                MappedDiagnosticsContext.Clear();
            }
        }