Esempio n. 1
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);
                }
            }
        }
 /// <summary>
 /// The private instantiation
 /// </summary>
 private EmailTargetConfigBuilder()
 {
     Config = new EmailTargetConfig();
 }