Example #1
0
            /// <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);
     }
 }
            /// <summary>
            /// Updates book keeping 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 (this.owner.rollSizeInBytes > 0 && this.managedWriter == null)
                {
                    currentWriter = this.owner.Writer as StreamWriter;

                    if (currentWriter == null)
                    {
                        // TWTL couldn't acquire the writer - abort
                        return(false);
                    }

                    var actualFileName = ((FileStream)currentWriter.BaseStream).Name;

                    currentWriter.Close();

                    FileStream fileStream = null;
                    try
                    {
                        fileStream         = File.Open(actualFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
                        this.managedWriter = new TallyKeepingFileStreamWriter(fileStream, GetEncodingWithFallback());
                    }
                    catch (IOException)
                    {
                        // 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;
                }

                // compute the next roll date if necessary
                if (this.owner.rollInterval != RollInterval.None && this.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
                        this.nextRollDateTime
                            = this.CalculateNextRollDate(File.GetCreationTime(((FileStream)((StreamWriter)this.owner.Writer).BaseStream).Name));
                    }
                    catch (IOException)
                    {
                        this.nextRollDateTime = DateTime.MaxValue; // disable rolling if no 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);
            }
        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);
            }
        }
Example #5
0
            public void PerformRoll(DateTime rollDateTime)
            {
                string actualFileName = ((FileStream)((StreamWriter)this.owner.Writer).BaseStream).Name;

                // calculate archive name
                string archiveFileName = this.ComputeArchiveFileName(actualFileName, rollDateTime);

                // close file
                this.owner.Writer.Close();
                // move file
                this.SafeMove(actualFileName, archiveFileName, rollDateTime);
                // update writer - let TWTL open the file as needed to keep consistency
                this.owner.Writer     = null;
                this.managedWriter    = null;
                this.nextRollDateTime = null;
                this.UpdateRollingInformationIfNecessary();
            }
        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);
            }
        }
        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>
            /// 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 PerformRoll(DateTime rollDateTime)
			{
				string actualFileName = ((FileStream)((StreamWriter)this.owner.Writer).BaseStream).Name;

				// calculate archive name
				string archiveFileName = this.ComputeArchiveFileName(actualFileName, rollDateTime);
				// close file
				this.owner.Writer.Close();
				// move file
				this.SafeMove(actualFileName, archiveFileName, rollDateTime);
				// update writer - let TWTL open the file as needed to keep consistency
				this.owner.Writer = null;
				this.managedWriter = null;
				this.nextRollDateTime = null;
				this.UpdateRollingInformationIfNecessary();
			}