Пример #1
0
		/// <summary>
		/// Initializes the target as normal, except with a layout built-into the target
		/// </summary>
		/// <param name="targetConfig">The target config to build this target from</param>
		/// <param name="dispatcher">The dispatcher this target is attached to</param>
		/// <param name="synchronous">An override to the synchronous setting in the target config</param>
		public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null)
		{
			// Initialize the base target as normal
			//  Add logic for initializing the layout as well
			base.Initialize(targetConfig, dispatcher, synchronous);
			if (targetConfig != null)
			{
				lock (_configLock)
				{
					// Check to see if the target configuration passed in is already a layout target configuration and initialize
					//  depending on this

					LayoutConfig layoutConfig = null;
					if (typeof(LayoutTargetConfig).IsAssignableFrom(targetConfig.GetType()))
					{
						layoutConfig = ((LayoutTargetConfig)targetConfig).LayoutConfig;
					}
					else
					{
						var layoutConfigToken = targetConfig.Config[LayoutTokenName];
						layoutConfig = new LayoutConfig(layoutConfigToken);
					}

					Layout = LayoutFactory.BuildLayout(layoutConfig);
				}
			}
			else
			{
				Name = DefaultTargetName;
				Layout = new StandardLayout();
			}
		}
Пример #2
0
        /// <summary>
        /// Initializes the target with the given targe tconfig, dispatcher and synchronous flag
        /// </summary>
        /// <param name="targetConfig">The configuration to build this target with</param>
        /// <param name="dispatcher">The dispatcher that this target is attached to</param>
        /// <param name="synchronous">The synchronous flag, used to overwrite the synchronous behavior in the configuration</param>
        public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null)
        {
            // Default to synchronous
            base.Initialize(targetConfig, dispatcher, synchronous.HasValue ? synchronous : true);

            // Parse out the target configuration
            if (targetConfig != null)
            {
                ConsoleTargetConfig consoleConfig = typeof(ConsoleTargetConfig).IsAssignableFrom(targetConfig.GetType())
                    ? (ConsoleTargetConfig)targetConfig
                    : new ConsoleTargetConfig(targetConfig.Config);

                ColorRules = consoleConfig.ColorRules;

                // The rules may have changed, reset the cache
                RuleCache.Clear();
            }
        }
Пример #3
0
		/// <summary>
		/// Initializes this target for writing to the windows event log.
		/// </summary>
		/// <param name="targetConfig">The target config to build this target from</param>
		/// <param name="dispatcher">The dispatcher this target is attached to</param>
		/// <param name="synchronous">An override to the synchronous setting in the target config</param>
		public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null)
		{
			base.Initialize(targetConfig, dispatcher, synchronous);

			// Parse out the target configuration
			Config = targetConfig != null &&
				typeof(WindowsEventLogTargetConfig).IsAssignableFrom(targetConfig.GetType())
					? (WindowsEventLogTargetConfig)targetConfig
					: new WindowsEventLogTargetConfig(targetConfig.Config);

			// Make sure that the windows event log is ready for us
			try
			{
				if (!EventLog.SourceExists(Config.Source))
					EventLog.CreateEventSource(Config.Source, Config.Log);
			}
			catch (Exception cause)
			{
				Debug.WriteLine("Failure ensuring log source exists, try running first as an administrator to create the source.  Error: " + cause.GetType().FullName + ": " + cause.Message);
			}
		}
Пример #4
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);
                }
            }
        }