public void WriterKeepsTally()
        {
            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day))
            {
                traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.Write("12345");

                Assert.AreEqual(5L, ((RollingFlatFileTraceListener.TallyKeepingFileStreamWriter)traceListener.Writer).Tally);
            }
        }
        public void RolledFileWillHaveCurrentDateForTimestamp()
        {
            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                   10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day))
            {
                traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;
                traceListener.Write("1234567890");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 01));
                traceListener.Write("12345");

                Assert.AreEqual(5L, ((RollingFlatFileTraceListener.TallyKeepingFileStreamWriter)traceListener.Writer).Tally);
            }

            Assert.IsTrue(File.Exists(fileName));
            Assert.AreEqual("12345", File.ReadAllText(fileName));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007" + extension));
            Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007" + extension));
        }
        public void WillRollForSize()
        {
            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                   1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year))
            {
                traceListener.RollingHelper.UpdateRollingInformationIfNecessary();

                traceListener.Write(new string('c', 1200));

                Assert.IsNotNull(traceListener.RollingHelper.CheckIsRollNecessary());
            }

            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                   0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year))
            {
                traceListener.RollingHelper.UpdateRollingInformationIfNecessary();

                traceListener.Write(new string('c', 1200));

                Assert.IsNull(traceListener.RollingHelper.CheckIsRollNecessary());
            }
        }
        public void RolledFileWithIncrementWillCreateArchiveFileWithMaxSequenceIfDateTemplateDoesMatch()
        {
            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                   0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day))
            {
                traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;
                traceListener.Write("1234567890");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 01));
                traceListener.Write("12345");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 02));
                traceListener.Write("abcde");
            }

            Assert.IsTrue(File.Exists(fileName));
            Assert.AreEqual("abcde", File.ReadAllText(fileName));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.2" + extension));
            Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2007.2" + extension));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.1" + extension));
            Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007.1" + extension));

            string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007*" + extension + "*");
            Assert.AreEqual(2, archiveFiles.Length);
        }
        public void RolledFileWithOverwriteWillFallBackToUniqueNameIfDateTemplateMatchesButArchiveFileIsInUse()
        {
            string targetArchiveFile = fileNameWithoutExtension + ".2007" + extension;

            using (FileStream stream = File.Open(targetArchiveFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
            {
                using (RollingFlatFileTraceListener traceListener
                    = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                       0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day))
                {
                    traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;
                    traceListener.Write("1234567890");

                    Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                    traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 01));
                    traceListener.Write("12345");
                }
            }

            Assert.IsTrue(File.Exists(fileName));
            Assert.AreEqual("12345", File.ReadAllText(fileName));
            Assert.IsTrue(File.Exists(targetArchiveFile));
            Assert.AreEqual("", File.ReadAllText(targetArchiveFile)); // couldn't archive

            string[] archiveFiles = Directory.GetFiles(".", targetArchiveFile + "*");
            Assert.AreEqual(2, archiveFiles.Length);
            foreach (string archiveFile in archiveFiles)
            {
                if (!Path.GetFileName(archiveFile).Equals(targetArchiveFile))
                {
                    Assert.AreEqual("1234567890", File.ReadAllText(archiveFile));
                }
            }
        }
        public void FallbackFileNameIsUsedForRoll()
        {
            using (FileStream fileStream = File.Open(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
            {
                using (RollingFlatFileTraceListener traceListener
                    = new RollingFlatFileTraceListener(fileName, "header", "footer", null,
                                                       10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day))
                {
                    traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;
                    traceListener.Write("1234567890");

                    Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                    traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 01));
                    traceListener.Write("12345");

                    Assert.AreEqual(5L, ((RollingFlatFileTraceListener.TallyKeepingFileStreamWriter)traceListener.Writer).Tally);
                }
            }
        }
        public void RolledFileWithOverwriteAndCapWillCreateArchiveFileIfDateTemplateDoesNotMatchAndKeepTheNewest()
        {
            using (RollingFlatFileTraceListener traceListener
                = new RollingFlatFileTraceListener(
                    fileName,
                    "header",
                    "footer",
                    null,
                    0,
                    "yyyy",
                    RollFileExistsBehavior.Overwrite,
                    RollInterval.Day,
                    1))
            {
                traceListener.RollingHelper.DateTimeProvider = dateTimeProvider;
                traceListener.Write("1234567890");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2007, 01, 01));
                traceListener.Write("12345");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2008, 01, 01));
                traceListener.Write("abcde");

                Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary());

                traceListener.RollingHelper.PerformRoll(new DateTime(2009, 01, 01));
                traceListener.Write("edcbe");
            }

            Assert.IsTrue(File.Exists(fileName));
            Assert.AreEqual("edcbe", File.ReadAllText(fileName));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2009" + extension));
            Assert.AreEqual("abcde", File.ReadAllText(fileNameWithoutExtension + ".2009" + extension));
            Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2008" + extension));
            Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2007" + extension));
        }