public void WhenMaxArchivedFilesExceedsAndRollForInterval()
        {
            var fileNameWithoutExtension = @".\Logs12\TestMaxArchivedFiles_RollInterval";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs12");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, new EventTextFormatter(EventTextFormatter.DashSeparator), 2);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Informational("Message 1");
                    logger.Informational("Message 2");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(2);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 3");
                    logger.Informational("Message 4");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(4);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 5");
                    logger.Informational("Message 6");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(6);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 7");
                    logger.Informational("Message 8");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(8);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 9");
                    logger.Informational("Message 10");
                });

            Assert.AreEqual<int>(3, FlatFileHelper.GetFileNames(fileNameWithoutExtension + "*.log").Count());
            Assert.IsFalse(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".1" + ".log"));
            Assert.IsFalse(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".2" + ".log"));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".3" + ".log"));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".4" + ".log"));
        }
        public void WhenMaxArchivedFilesExceedsAndRollingACrossDifferentDatesWithRollInterval()
        {
            var fileNameWithoutExtension = @".\Logs13\TestMaxArchivedFiles_RollInterval_CreatedOnDifferentDates";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs13");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            string existingMessage = "Existing Message";
            DateTime currentDateTime = new DateTime(2010, 1, 1);
            Directory.CreateDirectory(@".\Logs13");
            File.WriteAllText(fileName, existingMessage);
            File.SetCreationTime(fileName, currentDateTime);
            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, new EventTextFormatter(EventTextFormatter.DashSeparator), 2);
                    listener.EnableEvents(logger, EventLevel.LogAlways);

                    dateTimeProvider.OverrideCurrentDateTime = currentDateTime;
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 1");
                    dateTimeProvider.OverrideCurrentDateTime = currentDateTime.AddDays(4);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;

                    logger.Informational("Message 2");

                    dateTimeProvider.OverrideCurrentDateTime = currentDateTime.AddDays(6);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 3");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(4);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 4");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(6);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 5");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(8);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 6");
                });

            var files = FlatFileHelper.GetFileNames(fileNameWithoutExtension + "*.log");
            Assert.IsFalse(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".1" + ".log"));
            Assert.AreEqual<int>(3, files.Count());
        }
        public void WhenRollAtMidnight()
        {
            var fileNameWithoutExtension = @".\Logs9\WhenRollInterval_Midnight";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs9");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();
            DateTime? nextRoll = null;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Midnight, new EventTextFormatter(EventTextFormatter.DashSeparator));
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Informational("Message 1");
                    logger.Informational("Message 2");

                    nextRoll = DateTime.Now.AddDays(1);

                    dateTimeProvider.OverrideCurrentDateTime = nextRoll;
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;

                    logger.Informational("Message 3");
                    logger.Informational("Message 4");
                });

            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".log"));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + dateTimeProvider.OverrideCurrentDateTime.Value.Year + ".1" + ".log"));
            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + ".log").Contains("Message 4"));
            Assert.IsFalse(File.ReadAllText(fileNameWithoutExtension + ".log").Contains("Message 1"));
            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + "." + dateTimeProvider.OverrideCurrentDateTime.Value.Year + ".1" + ".log").Contains("Message 1"));
        }
        public void WhenRollIntervalExceedsMultipleTimes()
        {
            var fileNameWithoutExtension = @".\Logs10\RollForTime_MultipleLogs";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs10");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 10, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Minute, new EventTextFormatter(EventTextFormatter.DashSeparator));
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Informational("Message 1");
                    logger.Informational("Message 2");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddMinutes(2);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 3");
                    logger.Informational("Message 4");

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddMinutes(4);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 5");
                    logger.Informational("Message 6");
                });

            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + ".log").Contains("Message 6"));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".2" + ".log"));
            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + "." + DateTime.Now.Year + ".2" + ".log").Contains("Message 3"));
        }
        public void WhenRollInIncrementModeBasedOnInterval()
        {
            var fileNameWithoutExtension = @".\Logs2\RollFileIncrement";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs2");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 10, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, new EventTextFormatter(EventTextFormatter.DashSeparator));
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Informational("Message 1");

                    MockDateTimeProvider dtp = new MockDateTimeProvider();
                    dtp.OverrideCurrentDateTime = DateTime.Now.AddDays(1);
                    subscription.Sink.RollingHelper.DateTimeProvider = dtp;

                    logger.Informational("Message 2");
                });

            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".log"));
            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + ".log").Contains("Message 2"));

            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".1" + ".log"));
            Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + "." + DateTime.Now.Year + ".1" + ".log").Contains("Message 1"));
        }
        public void WhenRollOverwrite_Timestamp_RollSizeNone_RollIntervalNone()
        {
            var fileNameWithoutExtension = "RollOverwrite_Timestamp_RollSizeNone_RollIntervalNone";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteCreatedLogFiles(fileNameWithoutExtension);
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            string existingMessage = "Existing Message";
            File.WriteAllText(fileName, existingMessage);
            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.None, new EventTextFormatter());
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    logger.Informational("Message 1");
                    logger.Informational("Message 2");
                    logger.Informational("Message 3");
                    logger.Informational("Message 4");
                    logger.Informational("Message 5");
                    logger.Informational("Message 6");
                    logger.Informational("Message 7");
                    logger.Informational("Message 8");
                    logger.Informational("Message 9");
                    logger.Informational("Message 10");
                });

            Assert.IsTrue(File.ReadAllText(fileName).Contains("Message 10"));
            Assert.IsTrue(File.ReadAllText(fileName).Contains("Existing Message"));
        }
        public void WhenRollWithSizeAndRollIntervalNoneWithOverwrite()
        {
            var fileNameWithoutExtension = "RollOverwrite_Timestamp_RollSize_RollIntervalNone";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteCreatedLogFiles(fileNameWithoutExtension);
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            string existingMessage = "Existing Message";
            File.WriteAllText(fileName, existingMessage);
            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToRollingFlatFile(fileName, 1, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.None, new EventTextFormatter());
                    listener.EnableEvents(logger, EventLevel.LogAlways);

                    for (int msg = 0; msg < 26; msg++)
                    {
                        logger.Informational("Message " + msg.ToString());
                    }
                });

            Assert.IsTrue(File.ReadAllText(fileName).Contains("Message 25"));
            Assert.IsFalse(File.ReadAllText(fileName).Contains("Existing Message"));
        }
        public void RollFileDateUsesFileCreationDate()
        {
            var fileNameWithoutExtension = "RollDateBasedOnLogCreationDate";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteCreatedLogFiles(fileNameWithoutExtension);
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            string existingMessage = "Existing Message";
            DateTime currentDateTime = new DateTime(2011, 1, 1);
            File.WriteAllText(fileName, existingMessage);
            File.SetCreationTime(fileName, currentDateTime);
            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, new EventTextFormatter());
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    dateTimeProvider.OverrideCurrentDateTime = currentDateTime;
                    dateTimeProvider.OverrideCurrentDateTime = currentDateTime.AddDays(2);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;

                    logger.Informational("New message");
                });

            Assert.AreEqual(existingMessage, File.ReadAllText(fileNameWithoutExtension + ".2011.1" + ".log"));
            Assert.IsTrue(File.ReadAllText(fileName).Contains("New message"));
        }
        public void WhenRollIntervalExceedsBeforeEvents()
        {
            var fileNameWithoutExtension = @".\Logs1\Roll_WhenNoEntryInLogFile";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteDirectory(@".\Logs1");
            var logger = MockEventSource.Logger;
            var dateTimeProvider = new MockDateTimeProvider();

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    var subscription = listener.LogToRollingFlatFile(fileName, 1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, new EventTextFormatter(EventTextFormatter.DashSeparator));
                    listener.EnableEvents(logger, EventLevel.LogAlways);

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.ToLocalTime();
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;
                    subscription.Sink.RollingHelper.UpdateRollingInformationIfNecessary();

                    dateTimeProvider.OverrideCurrentDateTime = DateTime.Now.AddDays(1);
                    subscription.Sink.RollingHelper.DateTimeProvider = dateTimeProvider;

                    logger.Informational("Message 1");
                    logger.Informational("Message 2");
                });

            Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".log"));
            Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + dateTimeProvider.OverrideCurrentDateTime.Value.Year + ".1" + ".log"));
            StringAssert.Contains(File.ReadAllText(fileNameWithoutExtension + ".log"), "Payload : [message : Message 2]");
        }