Example #1
0
        private void FileTarget_ArchiveNumbering_DateAndSequenceTests(bool enableCompression)
        {
            var tempPath = ArchiveFilenameHelper.GenerateTempPath();
            var tempFile = Path.Combine(tempPath, "file.txt");
            var archiveExtension = enableCompression ? "zip" : "txt";
            try
            {
                var ft = new FileTarget
                {
#if NET4_5
                    EnableArchiveFileCompression = enableCompression,
#endif
                    FileName = tempFile,
                    ArchiveFileName = Path.Combine(tempPath, "archive/{#}." + archiveExtension),
                    ArchiveDateFormat = "yyyy-MM-dd",
                    ArchiveAboveSize = 1000,
                    LineEnding = LineEndingMode.LF,
                    Layout = "${message}",
                    MaxArchiveFiles = 3,
                    ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
                    ArchiveEvery = FileArchivePeriod.Day
                };

                SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);

                // we emit 5 * 250 *(3 x aaa + \n) bytes
                // so that we should get a full file + 3 archives
                Generate1000BytesLog('a');
                Generate1000BytesLog('b');
                Generate1000BytesLog('c');
                Generate1000BytesLog('d');
                Generate1000BytesLog('e');

                string archiveFilename = DateTime.Now.ToString(ft.ArchiveDateFormat);

                LogManager.Configuration = null;


#if NET4_5
                var assertFileContents = enableCompression ? new Action<string, string, Encoding>(AssertZipFileContents) : AssertFileContents;
#else
                var assertFileContents = new Action<string, string, Encoding>(AssertFileContents);
#endif
                ArchiveFilenameHelper helper = new ArchiveFilenameHelper(Path.Combine(tempPath, "archive"), archiveFilename, archiveExtension);

                AssertFileContents(tempFile,
                    StringRepeat(250, "eee\n"),
                    Encoding.UTF8);

                assertFileContents(helper.GetFullPath(1), StringRepeat(250, "bbb\n"), Encoding.UTF8);
                AssertFileSize(helper.GetFullPath(1), ft.ArchiveAboveSize);

                assertFileContents(helper.GetFullPath(2), StringRepeat(250, "ccc\n"), Encoding.UTF8);
                AssertFileSize(helper.GetFullPath(2), ft.ArchiveAboveSize);

                assertFileContents(helper.GetFullPath(3), StringRepeat(250, "ddd\n"), Encoding.UTF8);
                AssertFileSize(helper.GetFullPath(3), ft.ArchiveAboveSize);

                Assert.False(helper.Exists(0), "First archive should have been deleted due to max archive count.");
                Assert.False(helper.Exists(4), "Fifth archive must not have been created yet.");
            }
            finally
            {
                LogManager.Configuration = null;
                if (File.Exists(tempFile))
                    File.Delete(tempFile);
                if (Directory.Exists(tempPath))
                    Directory.Delete(tempPath, true);
            }
        }