public void When_many_files_written_over_dates_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Foo1");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.WriteLine("Foo2");
                logger.GetDate = () => new DateTime(2010, 10, 3);
                logger.WriteLine("Foo3");
                logger.GetDate = () => new DateTime(2010, 10, 4);
                logger.WriteLine("Foo4");
                logger.GetDate = () => new DateTime(2010, 10, 5);
                logger.WriteLine("Foo5");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-03_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Foo3{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-04_0.txt", Path.GetFileName(second));
                Assert.AreEqual($"Foo4{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-05_0.txt", Path.GetFileName(third));
                Assert.AreEqual($"Foo5{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_file_already_exists_with_wrong_date_a_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger1 = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger1.WriteLine("Foo");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 2)
                };
                logger2.WriteLine("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual($"Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_many_files_written_over_size_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2, maxFileSize: 5)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Long text0");
                logger.WriteLine("Long text1");
                logger.WriteLine("Long text2");
                logger.WriteLine("Long text3");
                logger.WriteLine("Long text4");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_2.txt", Path.GetFileName(first));
                Assert.AreEqual($"Long text2{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_3.txt", Path.GetFileName(second));
                Assert.AreEqual($"Long text3{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-01_4.txt", Path.GetFileName(third));
                Assert.AreEqual($"Long text4{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_file_already_exists_and_is_too_large_a_new_sequence_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var dateTime = new DateTime(2010, 10, 1);
                var logger1  = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger1.Write("Some long text");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger2.Write("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Some long text\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
 public void When_file_already_exists_that_file_is_written_to()
 {
     using (var tempPath = new TempPath())
     {
         var dateTime = new DateTime(2010, 10, 1);
         var logger1  = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger1.WriteLine("Foo");
         var files1 = tempPath.GetFiles();
         Assert.AreEqual(1, files1.Count);
         Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files1.First()));
         var logger2 = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger2.WriteLine("Bar");
         var files2 = tempPath.GetFiles();
         Assert.AreEqual(1, files2.Count);
         Assert.AreEqual($"Foo{Environment.NewLine}Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files2.First()));
     }
 }
 public void When_file_already_exists_that_file_is_written_to()
 {
     using (var tempPath = new TempPath())
     {
         var dateTime = new DateTime(2010, 10, 1);
         var logger1 = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger1.Write("Foo");
         var files1 = tempPath.GetFiles();
         Assert.AreEqual(1, files1.Count);
         Assert.AreEqual("Foo\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files1.First()));
         var logger2 = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger2.Write("Bar");
         var files2 = tempPath.GetFiles();
         Assert.AreEqual(1, files2.Count);
         Assert.AreEqual("Foo\r\nBar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files2.First()));
     }
 }
 public void When_many_sequence_files_are_written_the_max_is_not_exceeded()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
         {
             GetDate = () => new DateTime(2010, 10, 1)
         };
         for (var i = 0; i < 100; i++)
         {
             logger.WriteLine("Some long text");
             Assert.LessOrEqual(tempPath.GetFiles().Count, 11);
         }
     }
 }
        public void When_max_file_size_is_exceeded_sequence_number_is_added()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Some long text");
                logger.WriteLine("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Some long text{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual($"Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_date_changes_new_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Foo");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.Write("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Foo\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_many_files_written_over_dates_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Foo1");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.Write("Foo2");
                logger.GetDate = () => new DateTime(2010, 10, 3);
                logger.Write("Foo3");
                logger.GetDate = () => new DateTime(2010, 10, 4);
                logger.Write("Foo4");
                logger.GetDate = () => new DateTime(2010, 10, 5);
                logger.Write("Foo5");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-03_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Foo3\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-04_0.txt", Path.GetFileName(second));
                Assert.AreEqual("Foo4\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-05_0.txt", Path.GetFileName(third));
                Assert.AreEqual("Foo5\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_many_files_written_over_size_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2, maxFileSize: 5)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Long text0");
                logger.Write("Long text1");
                logger.Write("Long text2");
                logger.Write("Long text3");
                logger.Write("Long text4");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_2.txt", Path.GetFileName(first));
                Assert.AreEqual("Long text2\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_3.txt", Path.GetFileName(second));
                Assert.AreEqual("Long text3\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-01_4.txt", Path.GetFileName(third));
                Assert.AreEqual("Long text4\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_date_changes_new_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Foo");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.Write("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Foo\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
 public void When_many_sequence_files_are_written_the_max_is_not_exceeded()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
         {
             GetDate = () => new DateTime(2010, 10, 1)
         };
         for (var i = 0; i < 100; i++)
         {
             logger.Write("Some long text");
             Assert.LessOrEqual(tempPath.GetFiles().Count, 11);
         }
     }
 }
        public void When_max_file_size_is_exceeded_sequence_number_is_added()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Some long text");
                logger.Write("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Some long text\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_file_already_exists_with_wrong_date_a_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger1 = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger1.Write("Foo");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 2)
                };
                logger2.Write("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Foo\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_file_already_exists_and_is_too_large_a_new_sequence_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var dateTime = new DateTime(2010, 10, 1);
                var logger1 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger1.Write("Some long text");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger2.Write("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Some long text\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }