/// <summary>
        /// Initializes a new instance of the <see cref="RollingFlatFileTraceListener"/> class.
        /// </summary>
        /// <param name="fileName">The filename where the entries will be logged.</param>
        /// <param name="header">The header to add before logging an entry.</param>
        /// <param name="footer">The footer to add after logging an entry.</param>
        /// <param name="formatter">The formatter.</param>
        /// <param name="rollSizeKB">The maxium file size (KB) before rolling.</param>
        /// <param name="timeStampPattern">The date format that will be appended to the new roll file.</param>
        /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the roll file has to be created.</param>
        /// <param name="rollInterval">The time interval that makes the file rolles.</param>
        /// <param name="maxArchivedFiles">The maximum number of archived files to keep.</param>
        public RollingFlatFileTraceListener(string fileName,
                                            string header           = DefaultSeparator,
                                            string footer           = DefaultSeparator,
                                            ILogFormatter formatter = null,
                                            int rollSizeKB          = 0,
                                            string timeStampPattern = "yyyy-MM-dd",
                                            RollFileExistsBehavior rollFileExistsBehavior = RollFileExistsBehavior.Overwrite,
                                            RollInterval rollInterval = RollInterval.None,
                                            int maxArchivedFiles      = 0)
            : base(fileName, header, footer, formatter)
        {
            Guard.ArgumentNotNullOrEmpty(fileName, "fileName");

            this.rollSizeInBytes        = rollSizeKB * 1024;
            this.timeStampPattern       = timeStampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval           = rollInterval;
            this.maxArchivedFiles       = maxArchivedFiles;

            this.rollingHelper = new StreamWriterRollingHelper(this);

            if (rollInterval == RollInterval.Midnight)
            {
                var now      = this.rollingHelper.DateTimeProvider.CurrentDateTime;
                var midnight = now.AddDays(1).Date;

                this.timer = new Timer((o) => this.rollingHelper.RollIfNecessary(), null, midnight.Subtract(now), TimeSpan.FromDays(1));
            }
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RollingFlatFileSink"/> class with the specified values.
        /// </summary>
        /// <param name="fileName">The filename where the entries will be logged.</param>
        /// <param name="rollSizeKB">The maximum file size (KB) before rolling.</param>
        /// <param name="timestampPattern">The date format that will be appended to the new roll file.</param>
        /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the roll file has to be created.</param>
        /// <param name="rollInterval">The time interval that makes the file to be rolled.</param>
        /// <param name="maxArchivedFiles">The maximum number of archived files to keep.</param>
        /// <param name="formatter">The event entry formatter.</param>
        /// <param name="isAsync">Specifies if the writing should be done asynchronously, or synchronously with a blocking call.</param>
        public RollingFlatFileSink(string fileName, int rollSizeKB, string timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, int maxArchivedFiles, IEventTextFormatter formatter, bool isAsync)
        {
            Guard.ArgumentNotNull(formatter, "formatter");

            this.file      = FileUtil.ProcessFileNameForLogging(fileName);
            this.formatter = formatter;

            if (rollInterval == RollInterval.None)
            {
                if (!string.IsNullOrWhiteSpace(timestampPattern))
                {
                    Guard.ValidateTimestampPattern(timestampPattern, "timestampPattern");
                }
            }
            else
            {
                Guard.ValidateTimestampPattern(timestampPattern, "timestampPattern");
            }

            this.writer = new TallyKeepingFileStreamWriter(this.file.Open(FileMode.Append, FileAccess.Write, FileShare.Read));

            this.rollSizeInBytes        = rollSizeKB * 1024L;
            this.timestampPattern       = timestampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval           = rollInterval;
            this.maxArchivedFiles       = maxArchivedFiles;
            this.isAsync = isAsync;

            this.rollingHelper = new StreamWriterRollingHelper(this);

            if (rollInterval == RollInterval.Midnight && !isAsync)
            {
                var now      = this.rollingHelper.DateTimeProvider.CurrentDateTime;
                var midnight = now.AddDays(1).Date;

                var callback = new TimerCallback(delegate
                {
                    lock (this.lockObject)
                    {
                        this.rollingHelper.RollIfNecessary();
                    }
                });

                this.timer = new Timer(callback, null, midnight.Subtract(now), TimeSpan.FromDays(1));
            }

            this.flushSource.SetResult(true);
            if (isAsync)
            {
                this.cancellationTokenSource = new CancellationTokenSource();
                this.pendingEntries          = new BlockingCollection <EventEntry>();
                this.asyncProcessorTask      = Task.Factory.StartNew(this.WriteEntries, TaskCreationOptions.LongRunning);
            }
        }
        public RollingXmlTraceListener(
                string fileName,
                int rollSizeKB,
                string timeStampPattern,
                RollFileExistsBehavior rollFileExistsBehavior,
                RollInterval rollInterval,
                int maxArchivedFiles)
            : base(fileName)
        {
            this.rollSizeInBytes = rollSizeKB * 1024;
            this.timeStampPattern = timeStampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval = rollInterval;
            this.maxArchivedFiles = maxArchivedFiles;

            this.rollingHelper = new StreamWriterRollingHelper(this);
        }
Exemple #4
0
        /// <summary>
        /// Initializes a new instance of <see cref="RollingFlatFileTraceListener"/>
        /// </summary>
        /// <param name="fileName">The filename where the entries will be logged.</param>
        /// <param name="header">The header to add before logging an entry.</param>
        /// <param name="footer">The footer to add after logging an entry.</param>
        /// <param name="formatter">The formatter.</param>
        /// <param name="rollSizeKB">The maxium file size (KB) before rolling.</param>
        /// <param name="timeStampPattern">The date format that will be appended to the new roll file.</param>
        /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the rool file has to be created.</param>
        /// <param name="rollInterval">The time interval that makes the file rolles.</param>
        public RollingFlatFileTraceListener(string fileName,
                                            string header,
                                            string footer,
                                            ILogFormatter formatter,
                                            int rollSizeKB,
                                            string timeStampPattern,
                                            RollFileExistsBehavior rollFileExistsBehavior,
                                            RollInterval rollInterval)
            : base(fileName, header, footer, formatter)
        {
            this.rollSizeInBytes        = rollSizeKB * 1024;
            this.timeStampPattern       = timeStampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval           = rollInterval;

            this.rollingHelper = new StreamWriterRollingHelper(this);
        }
		/// <summary>
		/// Initializes a new instance of <see cref="RollingFlatFileTraceListener"/> 
		/// </summary>
		/// <param name="fileName">The filename where the entries will be logged.</param>
		/// <param name="header">The header to add before logging an entry.</param>
		/// <param name="footer">The footer to add after logging an entry.</param>
		/// <param name="formatter">The formatter.</param>
		/// <param name="rollSizeKB">The maxium file size (KB) before rolling.</param>
		/// <param name="timeStampPattern">The date format that will be appended to the new roll file.</param>
		/// <param name="rollFileExistsBehavior">Expected behavior that will be used when the rool file has to be created.</param>
		/// <param name="rollInterval">The time interval that makes the file rolles.</param>
		public RollingFlatFileTraceListener(string fileName,
			string header,
			string footer,
			ILogFormatter formatter,
			int rollSizeKB,
			string timeStampPattern,
			RollFileExistsBehavior rollFileExistsBehavior,
			RollInterval rollInterval)
			: base(fileName, header, footer, formatter)
		{
			this.rollSizeInBytes = rollSizeKB * 1024;
			this.timeStampPattern = timeStampPattern;
			this.rollFileExistsBehavior = rollFileExistsBehavior;
			this.rollInterval = rollInterval;

			this.rollingHelper = new StreamWriterRollingHelper(this);
		}
Exemple #6
0
        public RollingXmlTraceListener(
            string fileName,
            int rollSizeKB,
            string timeStampPattern,
            RollFileExistsBehavior rollFileExistsBehavior,
            RollInterval rollInterval,
            int maxArchivedFiles)
            : base(fileName)
        {
            this.rollSizeInBytes        = rollSizeKB * 1024;
            this.timeStampPattern       = timeStampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval           = rollInterval;
            this.maxArchivedFiles       = maxArchivedFiles;

            this.rollingHelper = new StreamWriterRollingHelper(this);
        }
Exemple #7
0
        public RollingFileTraceListener(
            string fileName,
            string name,
            int rollSizeKB)
            : base(OpenTextWriter(fileName), name)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(Resources.CannotBeNullOrEmpty, nameof(fileName));
            }

            if (rollSizeKB <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(rollSizeKB));
            }

            this.TraceFileName   = fileName;
            this.rollSizeInBytes = rollSizeKB * 1024;
            this.rollingHelper   = new StreamWriterRollingHelper(this);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RollingFlatFileTraceListener"/> class.
        /// </summary>
        /// <param name="fileName">The filename where the entries will be logged.</param>
        /// <param name="header">The header to add before logging an entry.</param>
        /// <param name="footer">The footer to add after logging an entry.</param>
        /// <param name="formatter">The formatter.</param>
        /// <param name="rollSizeKB">The maxium file size (KB) before rolling.</param>
        /// <param name="timeStampPattern">The date format that will be appended to the new roll file.</param>
        /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the roll file has to be created.</param>
        /// <param name="rollInterval">The time interval that makes the file rolles.</param>
        /// <param name="maxArchivedFiles">The maximum number of archived files to keep.</param>
        public RollingFlatFileTraceListener(string fileName,
                                            string header = DefaultSeparator,
                                            string footer = DefaultSeparator,
                                            ILogFormatter formatter = null,
                                            int rollSizeKB = 0,
                                            string timeStampPattern = "yyyy-MM-dd",
                                            RollFileExistsBehavior rollFileExistsBehavior = RollFileExistsBehavior.Overwrite,
                                            RollInterval rollInterval = RollInterval.None,
                                            int maxArchivedFiles = 0)
            : base(fileName, header, footer, formatter)
        {
            Guard.ArgumentNotNullOrEmpty(fileName, "fileName");

            this.rollSizeInBytes = rollSizeKB * 1024;
            this.timeStampPattern = timeStampPattern;
            this.rollFileExistsBehavior = rollFileExistsBehavior;
            this.rollInterval = rollInterval;
            this.maxArchivedFiles = maxArchivedFiles;

            this.rollingHelper = new StreamWriterRollingHelper(this);

            if (rollInterval == RollInterval.Midnight)
            {
                var now = this.rollingHelper.DateTimeProvider.CurrentDateTime;
                var midnight = now.AddDays(1).Date;

                this.timer = new Timer((o) => this.rollingHelper.RollIfNecessary(), null, midnight.Subtract(now), TimeSpan.FromDays(1));
            }
        }