Example #1
0
		/// <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>
 /// The private instantiation
 /// </summary>
 private TextFileTargetConfigBuilder()
 {
     Config = new TextFileTargetConfig();
 }