예제 #1
0
        public void SendsEmailMessagesForAllLogEntries() {
            var logMessages = new List<string>();

            using (ShimsContext.Create()) {
                // make DateTime.Now deterministic based on the number of log messages that have been written.
                ShimDateTime.NowGet = () => {
                    return DeterministicDateTime(logMessages.Count);
                };

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        logMessages.Add(e.Body);
                    };
                };

                using (var logWriter = new EmailLogger("test.server", LogLevel.All) {
                    Sender = new MailAddress("*****@*****.**"),
                    Asynchronous = false
                }) {
                    logWriter.AddRecipient("*****@*****.**");

                    logWriter.Write("the quick brown fox jumped over the lazy dog.", LogLevel.All);
                    logWriter.Write("Who are you people? Torchwood.", LogLevel.Warn);

                    var expectedResult = new List<string>() {
                        // log line for logLevel.All output
                        $"{ DeterministicDateTime(0).ToString(logWriter.TimestampFormat) }  the quick brown fox jumped over the lazy dog.",
                        $"{ DeterministicDateTime(1).ToString(logWriter.TimestampFormat) } [WARN] Who are you people? Torchwood.",
                    };

                    CollectionAssert.AreEqual(logMessages, expectedResult, "Log messages do not match.");
                }
            }
        }
예제 #2
0
        public void MessagesAreNotSentAtTheWrongLoggingLevel() {
            var logMessages = new List<string>();

            using (ShimsContext.Create()) {
                // make DateTime.Now deterministic based on the number of log messages that have been written.
                ShimDateTime.NowGet = () => {
                    return DeterministicDateTime(logMessages.Count);
                };

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        logMessages.Add(e.Body);
                    };
                };

                var messagesToSend = new List<KeyValuePair<LogLevel, string>>() {
                    new KeyValuePair<LogLevel, string>(LogLevel.Debug, "this is a debug message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Fatal, "this is a fatal message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Info, "this is an info message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.None, "this is a none message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Warn, "this is a warn message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.All, "this is an all message")
                };

                foreach (LogLevel lvl in Enum.GetValues(typeof(LogLevel))) {
                    // reset log messages
                    logMessages = new List<string>();

                    using (var logWriter = new EmailLogger("test.server", lvl) {
                        Sender = new MailAddress("*****@*****.**"),
                        Asynchronous = false
                    }) {
                        // write log messages
                        foreach(var m in messagesToSend.OrderBy(x => x.Key)) {
                            logWriter.Write(m.Value, m.Key);
                        }

                        // check log messages
                        var expectedResults = new List<string>();
                        int i = 0;
                        foreach(var m in messagesToSend.OrderBy(x => x.Key)) {
                            if (lvl != LogLevel.None && m.Key != LogLevel.None) {
                                if (lvl == LogLevel.All || m.Key <= lvl) {
                                    expectedResults.Add($"{ DeterministicDateTime(i).ToString(logWriter.TimestampFormat) } { ((m.Key == LogLevel.All) ? string.Empty : $"[{ m.Key.ToString().ToUpper() }]") } { m.Value }");
                                    i++;
                                }
                            }
                        }

                        CollectionAssert.AreEqual(logMessages, expectedResults, $"Failed for log level { lvl.ToString().ToUpper() }.");
                    }
                }
            }
        }
예제 #3
0
 public void ReplyToGetsSetCorrectly() {
     using (var logWriter = new EmailLogger("test.server", LogLevel.Warn)) {
         var newReplyTo = new MailAddress("*****@*****.**");
         logWriter.ReplyTo = newReplyTo;
         Assert.AreEqual(newReplyTo, logWriter.ReplyTo, "ReplyTo is not the expected value when it is set explicitly");
     }
 }
예제 #4
0
        public void ReplyToUsesSenderAddressWhenNotSet() {
            using (var logWriter = new EmailLogger("test.server", LogLevel.Warn)) {
                logWriter.Sender = new MailAddress("*****@*****.**");

                Assert.AreEqual(logWriter.Sender.Address, logWriter.ReplyTo.Address, "ReplyTo does not equal Sender when ReplyTo is not explicitly set.");
            }
        }