Пример #1
0
        /// <summary>Construct a <see cref="RollingFileSink"/>.</summary>
        /// <param name="pathFormat">String describing the location of the log files,
        /// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log
        /// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param>
        /// <param name="textFormatter">Formatter used to convert log events to text.</param>
        /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
        /// For unrestricted growth, pass null. The default is 1 GB.</param>
        /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
        /// including the current log file. For unlimited retention, pass null. The default is 31.</param>
        /// <param name="encoding">Character encoding used to write the text file. The default is UTF-8.</param>
        /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
        /// is false.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <remarks>The file will be written using the UTF-8 character set.</remarks>
        public RollingFileSink(string pathFormat,
                               ITextFormatter textFormatter,
                               long?fileSizeLimitBytes,
                               int?retainedFileCountLimit,
                               Encoding encoding = null,
                               bool buffered     = false)
        {
            if (pathFormat == null)
            {
                throw new ArgumentNullException(nameof(pathFormat));
            }
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
            {
                throw new ArgumentException("Negative value provided; file size limit must be non-negative");
            }
            if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1)
            {
                throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
            }

            _roller                 = new TemplatedPathRoller(pathFormat);
            _textFormatter          = textFormatter;
            _fileSizeLimitBytes     = fileSizeLimitBytes;
            _retainedFileCountLimit = retainedFileCountLimit;
            _encoding               = encoding ?? Encoding.UTF8;
            _buffered               = buffered;
        }
 public void OrderingMatchesFiles()
 {
     var roller = new TemplatedPathRoller("log-{Date}.txt");
     const string example = "log-20131210.txt";
     var matched = roller.OrderMatchingByAge(new[] { example });
     Assert.AreEqual(1, matched.Count());
 }
 public void OrderingPresentsNewerFilesFirst()
 {
     var roller = new TemplatedPathRoller("log-{Date}.txt");
     const string newer = "log-20150101.txt";
     const string older = "log-20141231.txt";
     var matched = roller.OrderMatchingByAge(new[] { older, newer });
     CollectionAssert.AreEqual(new[] { newer, older }, matched);
 }
 public void OrderingExcludesSimilarButNonmatchingFiles()
 {
     var roller = new TemplatedPathRoller("log-{Date}.txt");
     const string similar1 = "log-0.txt";
     const string similar2 = "log-helloyou.txt";
     var matched = roller.OrderMatchingByAge(new[] { similar1, similar2 });
     Assert.AreEqual(0, matched.Count());
 }
 public void TheLogFileIsNotRequiredToIncludeADirectory()
 {
     var roller = new TemplatedPathRoller("log-{Date}");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     roller.GetLogFilePath(now, 0, out path);
     AssertAreEqualAbsolute("log-20130714", path);
 }
 public void IfNoTokenIsSpecifiedDashFollowedByTheDateIsImplied()
 {
     var roller = new TemplatedPathRoller("Logs\\log.txt");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     roller.GetLogFilePath(now, 0, out path);
     AssertAreEqualAbsolute("Logs\\log-20130714.txt", path);
 }
 public void ANonZeroIncrementIsIncludedAndPadded()
 {
     var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     roller.GetLogFilePath(now, 12, out path);
     AssertAreEqualAbsolute("Logs\\log.20130714_012.txt", path);
 }
 public void TheLogFileIncludesDateToken()
 {
     var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     roller.GetLogFilePath(now, 0, out path);
     AssertAreEqualAbsolute("Logs\\log.20130714.txt", path);
 }
Пример #9
0
 public void TheLogFileIsNotRequiredToIncludeADirectory()
 {
     var roller = new TemplatedPathRoller("log-{Date}");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     DateTime nextCheckpoint;
     roller.GetLogFilePath(now, out path, out nextCheckpoint);
     Assert.AreEqual("log-20130714", path);
 }
 public void TheLogFileIncludesDateTokenAndSetsCheckpointToNextDay()
 {
     var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
     var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
     string path;
     DateTime nextCheckpoint;
     roller.GetLogFilePath(now, out path, out nextCheckpoint);
     AssertAreEqualAbsolute("Logs\\log.20130714.txt", path);
     Assert.AreEqual(new DateTime(2013, 7, 15), nextCheckpoint);
 }
Пример #11
0
        public RollingFileSink(string pathTemplate, 
                              ITextFormatter textFormatter,
                              long? fileSizeLimitBytes,
                              int? retainedFileCountLimit)
        {
            if (pathTemplate == null) throw new ArgumentNullException("pathTemplate");
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");
            if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");

            _roller = new TemplatedPathRoller(pathTemplate);
            _textFormatter = textFormatter;
            _fileSizeLimitBytes = fileSizeLimitBytes;
            _retainedFileCountLimit = retainedFileCountLimit;
        }
        /// <summary>Construct a <see cref="RollingFileSink"/>.</summary>
        /// <param name="pathFormat">String describing the location of the log files,
        /// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log
        /// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param>
        /// <param name="textFormatter">Formatter used to convert log events to text.</param>
        /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
        /// For unrestricted growth, pass null. The default is 1 GB.</param>
        /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
        /// including the current log file. For unlimited retention, pass null. The default is 31.</param>
        /// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
        /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
        /// is false.</param>
        /// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <remarks>The file will be written using the UTF-8 character set.</remarks>
        public RollingFileSink(string pathFormat,
                               ITextFormatter textFormatter,
                               long?fileSizeLimitBytes,
                               int?retainedFileCountLimit,
                               Encoding encoding = null,
                               bool buffered     = false,
                               bool shared       = false)
        {
            if (pathFormat == null)
            {
                throw new ArgumentNullException(nameof(pathFormat));
            }
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
            {
                throw new ArgumentException("Negative value provided; file size limit must be non-negative");
            }
            if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1)
            {
                throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
            }

#if !SHARING
            if (shared)
            {
                throw new NotSupportedException("File sharing is not supported on this platform.");
            }
#endif

            _roller                 = new TemplatedPathRoller(pathFormat);
            _textFormatter          = textFormatter;
            _fileSizeLimitBytes     = fileSizeLimitBytes;
            _retainedFileCountLimit = retainedFileCountLimit;
            _encoding               = encoding;
            _buffered               = buffered;
            _shared                 = shared;
        }
 public void TheDirectorSearchPatternUsesWildcardInPlaceOfDate()
 {
     var roller = new TemplatedPathRoller("Logs\\log-{Date}.txt");
     Assert.AreEqual("log-*.txt", roller.DirectorySearchPattern);
 }
 public void TheRollerReturnsTheLogFileDirectory()
 {
     var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
     AssertAreEqualAbsolute("Logs", roller.LogFileDirectory);
 }
 public void MatchingSelectsFiles()
 {
     var roller = new TemplatedPathRoller("log-{Date}.txt");
     const string example1 = "log-20131210.txt";
     const string example2 = "log-20131210_031.txt";
     var matched = roller.SelectMatches(new[] { example1, example2 }).ToArray();
     Assert.AreEqual(2, matched.Count());
     Assert.AreEqual(0, matched[0].SequenceNumber);
     Assert.AreEqual(31, matched[1].SequenceNumber);
 }
 public void MatchingParsesDates()
 {
     var roller = new TemplatedPathRoller("log-{Date}.txt");
     const string newer = "log-20150101.txt";
     const string older = "log-20141231.txt";
     var matched = roller.SelectMatches(new[] { older, newer }).OrderByDescending(m => m.Date).Select(m => m.Filename).ToArray();
     CollectionAssert.AreEqual(new[] { newer, older }, matched);
 }