/// <summary> /// Initializes a new instance of the <see cref="RollingFileLogProvider"/> class. This protected /// constructor exists to facilitate testing - its <paramref name="getCurrentTime"/> and /// <paramref name="getFileCreationTime"/> parameters allow a subclass to inject system clock /// and file system behavior. /// </summary> /// <param name="getCurrentTime">A function that gets the current time.</param> /// <param name="getFileCreationTime">A function that gets a file's creation time.</param> /// <param name="file">The path to a writable file.</param> /// <param name="formatter">An object that formats log entries prior to writing to file.</param> /// <param name="level">The level of the log provider.</param> /// <param name="timeout">The timeout of the log provider.</param> /// <param name="maxFileSizeKilobytes"> /// The maximum file size, in bytes, of the file. If the file size is greater than this value, /// it is archived. /// </param> /// <param name="maxArchiveCount"> /// The maximum number of archive files that will be kept. If the number of archive files is /// greater than this value, then they are deleted, oldest first. /// </param> /// <param name="rolloverPeriod"> /// The rollover period, indicating if/how the file should archived on a periodic basis. /// </param> protected RollingFileLogProvider( Func <DateTime> getCurrentTime, Func <FileInfo, DateTime> getFileCreationTime, string file, ILogFormatter formatter, LogLevel level = default(LogLevel), TimeSpan?timeout = null, int maxFileSizeKilobytes = DefaultMaxFileSizeKilobytes, int maxArchiveCount = DefaultMaxArchiveCount, RolloverPeriod rolloverPeriod = DefaultRolloverPeriod) : base(file, formatter, level, timeout) { if (maxFileSizeKilobytes < 0) { throw new ArgumentException("MaxFileSizeKilobytes cannot be negative.", nameof(maxFileSizeKilobytes)); } if (maxFileSizeKilobytes > (int.MaxValue / 1024)) { throw new ArgumentException($"MaxFileSizeKilobytes cannot be greater than {int.MaxValue / 1024}.", nameof(maxFileSizeKilobytes)); } if (maxArchiveCount < 0) { throw new ArgumentException("MaxArchiveCount cannot be negative.", nameof(maxArchiveCount)); } if (!Enum.IsDefined(typeof(RolloverPeriod), rolloverPeriod)) { throw new ArgumentException($"Rollover period is not defined: {rolloverPeriod}.", nameof(rolloverPeriod)); } _getCurrentTime = getCurrentTime ?? throw new ArgumentNullException(nameof(getCurrentTime)); _getFileCreationTime = getFileCreationTime ?? throw new ArgumentNullException(nameof(getFileCreationTime)); MaxFileSizeBytes = GetMaxFileSizeBytes(maxFileSizeKilobytes); MaxArchiveCount = maxArchiveCount; RolloverPeriod = rolloverPeriod; }
public TestingRollingFileLogProvider( string file, RolloverPeriod rolloverPeriod, params TimeSet[] timeSets) : base(GetCurrentTimeFunc(timeSets), GetFileCreationTimeFunc(timeSets), file, new TemplateLogFormatter("{level}:{message}"), rolloverPeriod: rolloverPeriod) { }
private static FileArchivePeriod ConvertRollingPeriod(RolloverPeriod period) { switch (period) { case RolloverPeriod.Day: return(FileArchivePeriod.Day); case RolloverPeriod.Hour: return(FileArchivePeriod.Hour); case RolloverPeriod.Minute: return(FileArchivePeriod.Minute); case RolloverPeriod.Month: return(FileArchivePeriod.Month); case RolloverPeriod.None: return(FileArchivePeriod.None); case RolloverPeriod.Year: return(FileArchivePeriod.Year); } throw new ArgumentException("Unexpected value - " + period); }
/// <summary> /// Initializes a new instance of the <see cref="RollingFileLogProvider"/> class. /// </summary> /// <param name="file">The path to a writable file.</param> /// <param name="formatter">An object that formats log entries prior to writing to file.</param> /// <param name="level">The level of the log provider.</param> /// <param name="timeout">The timeout of the log provider.</param> /// <param name="maxFileSizeKilobytes"> /// The maximum file size, in bytes, of the file. If the file size is greater than this value, /// it is archived. /// </param> /// <param name="maxArchiveCount"> /// The maximum number of archive files that will be kept. If the number of archive files is /// greater than this value, then they are deleted, oldest first. /// </param> /// <param name="rolloverPeriod"> /// The rollover period, indicating if/how the file should archived on a periodic basis. /// </param> public RollingFileLogProvider(string file, ILogFormatter formatter, LogLevel level = default(LogLevel), TimeSpan?timeout = null, int maxFileSizeKilobytes = DefaultMaxFileSizeKilobytes, int maxArchiveCount = DefaultMaxArchiveCount, RolloverPeriod rolloverPeriod = DefaultRolloverPeriod) : this(_defaultGetCurrentTime, _defaultGetFileCreationTime, file, formatter, level, timeout, maxFileSizeKilobytes, maxArchiveCount, rolloverPeriod) { }
/// <summary> /// Initializes a new instance of the <see cref="RollingFileLogProvider"/> class. /// </summary> /// <param name="file">The file that logs will be written to.</param> /// <param name="template">The template used to format log entries.</param> /// <param name="level">The level of the log provider.</param> /// <param name="timeout">The timeout of the log provider.</param> /// <param name="maxFileSizeKilobytes"> /// The maximum file size, in bytes, of the file. If the file size is greater than this value, /// it is archived. /// </param> /// <param name="maxArchiveCount"> /// The maximum number of archive files that will be kept. If the number of archive files is /// greater than this value, then they are deleted, oldest first. /// </param> /// <param name="rolloverPeriod"> /// The rollover period, indicating if/how the file should archived on a periodic basis. /// </param> public RollingFileLogProvider(string file, string template = DefaultTemplate, LogLevel level = default, TimeSpan?timeout = null, int maxFileSizeKilobytes = DefaultMaxFileSizeKilobytes, int maxArchiveCount = DefaultMaxArchiveCount, RolloverPeriod rolloverPeriod = DefaultRolloverPeriod) : this(_defaultGetCurrentTime, _defaultGetFileCreationTime, file, new TemplateLogFormatter(template ?? DefaultTemplate), level, timeout, maxFileSizeKilobytes, maxArchiveCount, rolloverPeriod) { }
public TestingRollingFileLogProvider( string file, RolloverPeriod rolloverPeriod, params TimeSet[] timeSets) : base( file, rolloverPeriod: rolloverPeriod, logFormatter: new SerializingLogFormatter(new XmlSerializerSerializer())) { _timeSets = timeSets; }
private static FileArchivePeriod ConvertRollingPeriod(RolloverPeriod period) { switch (period) { case RolloverPeriod.Day: return FileArchivePeriod.Day; case RolloverPeriod.Hour: return FileArchivePeriod.Hour; case RolloverPeriod.Minute: return FileArchivePeriod.Minute; case RolloverPeriod.Month: return FileArchivePeriod.Month; case RolloverPeriod.None: return FileArchivePeriod.None; case RolloverPeriod.Year: return FileArchivePeriod.Year; } throw new ArgumentException("Unexpected value - " + period); }