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);
 }
 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);
 }
Пример #6
0
        void OpenFile(DateTime now)
        {
            string   path;
            DateTime nextCheckpoint;

            _roller.GetLogFilePath(now, out path, out nextCheckpoint);
            _nextCheckpoint = nextCheckpoint;
            _currentFile    = new FileSink(path, _textFormatter, _fileSizeLimitBytes);
            ApplyRetentionPolicy(path);
        }
Пример #7
0
        void OpenFile(DateTime now)
        {
            var currentCheckpoint = _roller.GetCurrentCheckpoint(now);

            // We only take one attempt at it because repeated failures
            // to open log files REALLY slow an app down.
            _nextCheckpoint = _roller.GetNextCheckpoint(now);

            var existingFiles = Enumerable.Empty <string>();

            try
            {
                existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
                                .Select(Path.GetFileName);
            }
            catch (DirectoryNotFoundException) { }

            var latestForThisCheckpoint = _roller
                                          .SelectMatches(existingFiles)
                                          .Where(m => m.DateTime == currentCheckpoint)
                                          .OrderByDescending(m => m.SequenceNumber)
                                          .FirstOrDefault();

            var sequence = latestForThisCheckpoint != null ? latestForThisCheckpoint.SequenceNumber : 0;

            const int maxAttempts = 3;

            for (var attempt = 0; attempt < maxAttempts; attempt++)
            {
                string path;
                _roller.GetLogFilePath(now, sequence, out path);

                try
                {
                    _currentFile = _shared ?
                                   (ILogEventSink) new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
                                   new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered);
                }
                catch (IOException ex)
                {
                    if (IOErrors.IsLockedFile(ex))
                    {
                        SelfLog.WriteLine("Rolling file target {0} was locked, attempting to open next in sequence (attempt {1})", path, attempt + 1);
                        sequence++;
                        continue;
                    }

                    throw;
                }

                ApplyRetentionPolicy(path);
                return;
            }
        }
Пример #8
0
        void OpenFile(DateTime now)
        {
            var date = now.Date;

            // We only take one attempt at it because repeated failures
            // to open log files REALLY slow an app down.
            _nextCheckpoint = date.AddDays(1);

            var existingFiles = Enumerable.Empty <string>();

            try
            {
                existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
                                .Select(Path.GetFileName);
            }
            catch (DirectoryNotFoundException) { }

            var latestForThisDate = _roller
                                    .SelectMatches(existingFiles)
                                    .Where(m => m.Date == date)
                                    .OrderByDescending(m => m.SequenceNumber)
                                    .FirstOrDefault();

            var sequence = latestForThisDate != null ? latestForThisDate.SequenceNumber : 0;

            const int maxAttempts = 3;

            for (var attempt = 0; attempt < maxAttempts; attempt++)
            {
                string path;
                _roller.GetLogFilePath(now, sequence, out path);

                try
                {
                    _currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes);
                }
                catch (IOException ex)
                {
                    var errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
                    if (errorCode == 32 || errorCode == 33)
                    {
                        SelfLog.WriteLine("Rolling file target {0} was locked, attempting to open next in sequence (attempt {1})", path, attempt + 1);
                        sequence++;
                        continue;
                    }

                    throw;
                }

                ApplyRetentionPolicy(path);
                return;
            }
        }
 public void TheLogFileIsNotRequiredToIncludeAnExtension()
 {
     var roller = new TemplatedPathRoller("Logs\\log-{Date}");
     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", path);
 }