/// <summary> /// Perform the roll for the next date. /// </summary> /// <param name="rollDateTime">The roll date.</param> public void PerformRoll(DateTime rollDateTime) { string actualFileName = ((FileStream)((StreamWriter)this.owner.Writer).BaseStream).Name; // calculate archive name string directory = Path.GetDirectoryName(actualFileName); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(actualFileName); string extension = Path.GetExtension(actualFileName); StringBuilder fileNameBuilder = new StringBuilder(fileNameWithoutExtension); fileNameBuilder.Append('.'); fileNameBuilder.Append("bak"); fileNameBuilder.Append(extension); string archiveFileName = Path.Combine(directory, fileNameBuilder.ToString()); #if TODO // close file owner.Writer.Close(); #endif // move file SafeMove(actualFileName, archiveFileName, rollDateTime); // update writer - let TWTL open the file as needed to keep consistency this.owner.Writer = null; this.managedWriter = null; this.UpdateRollingInformationIfNecessary(); }
/// <summary> /// Perform the roll for the next date. /// </summary> /// <param name="rollDateTime">The roll date.</param> public void PerformRoll(DateTime rollDateTime) { string actualFileName = ((FileStream)((StreamWriter)owner.Writer).BaseStream).Name; if (this.owner.rollFileExistsBehavior == RollFileExistsBehavior.Overwrite && string.IsNullOrEmpty(this.owner.timeStampPattern)) { // no roll will be actually performed: no timestamp pattern is available, and // the roll behavior is overwrite, so the original file will be truncated owner.Writer.Close(); File.WriteAllText(actualFileName, string.Empty); } else { // calculate archive name string archiveFileName = ComputeArchiveFileName(actualFileName, rollDateTime); // close file owner.Writer.Close(); // move file SafeMove(actualFileName, archiveFileName, rollDateTime); // purge if necessary PurgeArchivedFiles(actualFileName); } // update writer - let TWTL open the file as needed to keep consistency owner.Writer = null; managedWriter = null; nextRollDateTime = null; UpdateRollingInformationIfNecessary(); }
public void InitializesTallyForNewFile() { using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Append, FileAccess.Write, FileShare.Read))) { Assert.AreEqual(0L, writer.Tally); } }
public void WritingToFileUpdatesTally() { using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Append, FileAccess.Write, FileShare.Read))) { writer.Write("12345"); writer.Flush(); Assert.AreEqual(5L, writer.Tally); } }
public void WritingToFileWithEncodingUpdatesTally() { using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF32)) { writer.Write("12345"); writer.Flush(); Assert.AreEqual(20L, writer.Tally); // BOM is not part of tally - minimal fidelity loss on new files. } }
/// <summary> /// Updates bookeeping information necessary for rolling, as required by the specified /// rolling configuration. /// </summary> /// <returns>true if update was successful, false if an error occurred.</returns> public bool UpdateRollingInformationIfNecessary() { StreamWriter currentWriter = null; // replace writer with the tally keeping version if necessary for size rolling if (owner.rollSizeInBytes > 0 && managedWriter == null) { currentWriter = owner.Writer as StreamWriter; if (currentWriter == null) { // TWTL couldn't acquire the writer - abort return(false); } String actualFileName = ((FileStream)currentWriter.BaseStream).Name; currentWriter.Close(); FileStream fileStream = null; try { fileStream = File.Open(actualFileName, FileMode.Append, FileAccess.Write, FileShare.Read); managedWriter = new TallyKeepingFileStreamWriter(fileStream, GetEncodingWithFallback()); } catch (Exception) { // there's a slight chance of error here - abort if this occurs and just let TWTL handle it without attempting to roll return(false); } owner.Writer = managedWriter; } // compute the next roll date if necessary if (owner.rollInterval != RollInterval.None && nextRollDateTime == null) { try { // casting should be safe at this point - only file stream writers can be the writers for the owner trace listener. // it should also happen rarely nextRollDateTime = CalculateNextRollDate(File.GetCreationTime(((FileStream)((StreamWriter)owner.Writer).BaseStream).Name)); } catch (Exception) { nextRollDateTime = DateTime.MaxValue; // disable rolling if not date could be retrieved. // there's a slight chance of error here - abort if this occurs and just let TWTL handle it without attempting to roll return(false); } } return(true); }
/// <summary> /// Updates bookeeping information necessary for rolling, as required by the specified /// rolling configuration. /// </summary> /// <returns>true if update was successful, false if an error occurred.</returns> public bool UpdateRollingInformationIfNecessary() { // replace writer with the tally keeping version if necessary for size rolling if (this.managedWriter == null) { try { this.managedWriter = OpenTextWriter(this.owner.TraceFileName); } catch (Exception) { // there's a slight chance of error here - abort if this occurs and just let TWTL handle it without attempting to roll return(false); } this.owner.Writer = this.managedWriter; } return(true); }
public void InitializesTallyForExistingFile() { File.WriteAllText(fileName, "12345"); using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Append, FileAccess.Write, FileShare.Read))) { Assert.AreEqual(5L, writer.Tally); } File.WriteAllText(fileName, "12345"); using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.Read))) { Assert.AreEqual(0L, writer.Tally); } File.WriteAllText(fileName, "12345"); using (var writer = new TallyKeepingFileStreamWriter(File.Open(fileName, FileMode.Truncate, FileAccess.Write, FileShare.Read))) { Assert.AreEqual(0L, writer.Tally); } }
/// <summary> /// Initializes a new instance of the <see cref="StreamWriterRollingHelper"/> class. /// </summary> /// <param name="owner"> /// The <see cref="RollingFileTraceListener"/> to use. /// </param> public StreamWriterRollingHelper(RollingFileTraceListener owner) { this.owner = owner; this.managedWriter = owner.Writer as TallyKeepingFileStreamWriter; }