Exemple #1
0
        public void TestTags()
        {
            var layout = new StandardLayout("${Tags}");

            string expected = "this,is,a,test";
            string actual = layout.FormatLogEvent(new LogEvent
            {
                Tags = new List<string> { "this", "is", "a", "test" }
            });

            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public void TestMessage()
        {
            var layout = new StandardLayout("${Message}");

            string guid = Guid.NewGuid().ToString();
            string actualString = layout.FormatLogEvent(new LogEvent
            {
                Message = guid
            });

            Assert.AreEqual(guid, actualString);
        }
Exemple #3
0
        public void TestDateTimeFormat()
        {
            var layout = new StandardLayout("${DateTime:'{0:yyyyMMdd}'}");

            var now = DateTime.Now;
            string actualString = layout.FormatLogEvent(new LogEvent
            {
                DateTime = now
            });

            var expectedString = now.ToString("yyyyMMdd");

            Assert.AreEqual(actualString, expectedString);
        }
Exemple #4
0
        public void TestContingent()
        {
            var layout = new StandardLayout("${Message}${?Exception.Message:' | {0}'}");

            string expectedWithError = "Hello, World! | Hello, Error!";
            string actualWithError = layout.FormatLogEvent(new LogEvent
            {
                Message = "Hello, World!",
                Exception = new Exception("Hello, Error!")
            });

            Assert.AreEqual(expectedWithError, actualWithError);

            string expectedWithoutError = "Hello, World!";
            string actualWithoutError = layout.FormatLogEvent(new LogEvent
            {
                Message = "Hello, World!"
            });

            Assert.AreEqual(expectedWithoutError, actualWithoutError);
        }
Exemple #5
0
        public void TestMetaData()
        {
            var layout = new StandardLayout("${HelloWorld:'{0}!'}");
            var actual = layout.FormatLogEvent(new LogEvent
            {
                MetaData = new Dictionary<string, object>
                {
                    {"HelloWorld", "Hello, World"}
                }
            });

            var expected = "Hello, World!";
            Assert.AreEqual(expected, actual);
        }
Exemple #6
0
        /// <summary>
        /// Initializes/Configures this email target
        /// </summary>
        /// <param name="targetConfig">The target config to use to configure this email target</param>
        /// <param name="dispatcher">The dispatcher this target is associated to</param>
        /// <param name="synchronous">An optional override to the synchronous flag in the target config</param>
        public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null)
        {
            // Wire up all the default goodies
            base.Initialize(targetConfig, dispatcher, synchronous);

            lock (_configLock)
            {
                // Reset our "Default" settings to "zero"
                FromAddress = null;
                ReplyTo.Clear();
                To.Clear();
                CC.Clear();
                BCC.Clear();

                if (targetConfig != null)
                {
                    // Check target config type here then get it into a SmtpTargetConfig
                    if (typeof(EmailTargetConfig).IsAssignableFrom(targetConfig.GetType()))
                    {
                        Config = (EmailTargetConfig)targetConfig;
                    }
                    else
                    {
                        Config = new EmailTargetConfig(targetConfig.Config);
                    }

                    // Build out the subject layout
                    SubjectLayout = LayoutFactory.BuildLayout(Config.SubjectLayout);

                    // Make sure body layout is setup correctly
                    //  If the body file is specified, load a StandardLayout with the contents of the file as the format
                    //  Otherwise, use the layout factory to build our layout
                    if (String.IsNullOrEmpty(Config.BodyFile))
                    {
                        BodyLayout = LayoutFactory.BuildLayout(Config.BodyLayout);
                    }
                    else
                    {
                        try
                        {
                            string fileContent = File.ReadAllText(Config.BodyFile);
                            BodyLayout = new StandardLayout(fileContent);
                        }
                        catch (Exception cause)
                        {
                            Trace.WriteLine(String.Format(FailedToLoadBodyFileMessage, Config.BodyFile, cause));
                            BodyLayout = new StandardLayout();
                        }
                    }

                    // Parse out the addresses
                    FromAddress = new MailAddress(Config.FromAddress);

                    // Parse the other email address lists
                    AddAddresses(ReplyTo, Config.ReplyTo);
                    AddAddresses(To, Config.To);
                    AddAddresses(CC, Config.CC);
                    AddAddresses(BCC, Config.BCC);
                }
            }
        }