/// <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(); } }
// Initialize our configuration public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null) { // We need to inherit the configuration of the parent targets base.Initialize(targetConfig, dispatcher, synchronous); // Cast or parse our configuration into a ColorConsoleTargetConfig if (targetConfig != null) ColorConfig = targetConfig is ColorConsoleTargetConfig ? (ColorConsoleTargetConfig)targetConfig : new ColorConsoleTargetConfig(targetConfig.Config); }
/// <summary> /// Initializes this text file target /// </summary> /// <param name="targetConfig">The configuration for this target</param> /// <param name="dispatcher">The dispatcher this target belongs to</param> /// <param name="synchronous">An override to the synchronous flag in the target config</param> public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null) { // Initialize the target using the base base.Initialize(targetConfig, dispatcher, synchronous); // Make sure we have an initialized text file target if (targetConfig != null) Config = targetConfig is TextFileTargetConfig ? (TextFileTargetConfig)targetConfig : new TextFileTargetConfig(targetConfig.Config); // Setup a bit. Make sure that the directory exists where we are going to write the log file. // Start a worker task for rolling the log file and cleaning the old files EnsurePathExists(Config.FileName); Task.Factory.StartNew(() => { // Run this task until the target is told to shutdown // Watch for the internal stopwatch running, which indicates that data has been written to log // Wait for the minimum idle time since the last write, or the maximum wait time to roll/cleanup the files Stopwatch maxWaitStopwatch = new Stopwatch(); while (!DoShutdown) { if (_sw.IsRunning) { if (maxWaitStopwatch.IsRunning == false) maxWaitStopwatch.Start(); if (_sw.ElapsedMilliseconds > RollIdleWait || maxWaitStopwatch.ElapsedMilliseconds > MaxRollWait) { lock (_fileLock) { RollFile(); CleanupOldFiles(); _sw.Stop(); maxWaitStopwatch.Stop(); } } } Thread.Yield(); Thread.Sleep(RollIdleWait); } }); }
/// <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(); } }
/// <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); } }
/// <summary> /// The observer hook for a new target configuration /// </summary> /// <param name="targetConfig">The new target configuration</param> public override void NotifyNewConfig(TargetConfig targetConfig) { Initialize(targetConfig); }
/// <summary> /// Adds a target to the configuration /// </summary> /// <param name="targetConfig">The target to add</param> /// <returns>This LoggingConfigBuilder</returns> public LoggingConfigBuilder AddTarget(TargetConfig targetConfig) { Targets.Add(targetConfig); return this; }
// Notify the target of a new config public override void NotifyNewConfig(TargetConfig targetConfig) { base.NotifyNewConfig(targetConfig); }
// Initialize the target public override void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null) { base.Initialize(targetConfig, dispatcher, synchronous); }
/// <summary> /// Hook to notify this target of a new configuration /// </summary> /// <param name="targetConfig">The new configuration for this target</param> public virtual void NotifyNewConfig(TargetConfig targetConfig) { Initialize(targetConfig); }
/// <summary> /// Initializes the target using the provided information /// </summary> /// <param name="targetConfig">The configuration to use for this target</param> /// <param name="dispatcher">The dispatcher this target belongs to</param> /// <param name="synchronous">An override of the synchronous flag in the target config</param> public virtual void Initialize(TargetConfig targetConfig, LogEventDispatcher dispatcher = null, bool? synchronous = null) { // Wire up our pieces BaseTargetConfig = targetConfig; Dispatcher = dispatcher; // use the passed configuration if we have it, otherwise, use default values if (targetConfig != null) { Name = targetConfig.Name; Synchronous = synchronous.HasValue ? synchronous.Value : targetConfig.Synchronous.HasValue ? targetConfig.Synchronous.Value : DefaultSynchronousSetting; } else { Name = DefaultName; Synchronous = synchronous.HasValue ? synchronous.Value : DefaultSynchronousSetting; } // Wire up the worker thread (will start a thread if synchronous is false, will stop it otherwise) ConfigureWorkerThread(Synchronous); }
/// <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); } } }