void CloseFile() { if (m_currentFile != null) { m_currentFile.Dispose(); m_currentFile = null; } m_nextCheckpoint = null; }
void OpenFile(DateTime now) { var date = now.Date; var latestFileForNow = EnumerateFiles().FirstOrDefault(v => v.Date == date); var sequence = (latestFileForNow?.SequenceNumber ?? 0) + 1; // Re-open the file on the next day. m_nextCheckpoint = date.AddDays(1); const int maxAttempts = 3; for (var attempt = 0; attempt < maxAttempts; attempt++) { var newFileName = ConstructLogFilename(now, sequence); try { m_currentFile = new SizedFileSink(newFileName, m_textFormatter, m_encoding); } catch (IOException ex) { var errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1); if (errorCode == 32 || errorCode == 33) { // "Rolling file target {0} was locked, attempting to open next in sequence (attempt {1})", newFileName, attempt + 1); sequence++; continue; } throw; } // Purge old files. ApplyRetentionPolicy(now, newFileName); return; } }
protected override bool ShouldRollFile(DateTime now, SizedFileSink currentFile) { // Close previous file if size reached. return(m_fileSizeLimitBytes.HasValue && currentFile.EstimatedLength >= m_fileSizeLimitBytes.Value); }
/// <summary> /// Return true if the current file should be closed. /// </summary> /// <param name="now">Time of the call</param> /// <param name="currentFile"></param> /// <returns></returns> protected abstract bool ShouldRollFile(DateTime now, SizedFileSink currentFile);