예제 #1
0
        public async System.Threading.Tasks.Task RespectsMaxFileCount()
        {
            this.TestContext.WriteLine($"{{ \r\n \"Location\" : \"{System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}\", ");

            this.TestContext.WriteLine($"\"TempPath\" : \"{this.TempPath}\", ");

            System.IO.Directory.CreateDirectory(this.TempPath);
            System.IO.File.WriteAllText(System.IO.Path.Combine(this.TempPath, "randomFile.txt"), "");

            var provider      = new Init.TestFileLoggerProvider(this.TempPath, maxRetainedFiles: 5);
            var logger        = (BatchLogger.BatchingLogger)provider.CreateLogger("Cat");
            var correlationId = new System.Guid("939221c1-c966-4762-8d6c-77b1e3fe29e3");

            await provider.IntervalControl.Pause;
            var timestamp = this._timestampOne;

            for (var i = 0; i < 10; i++)
            {
                logger.Log(timestamp, Microsoft.Extensions.Logging.LogLevel.Information, 0, "Info message", null,
                           (state, ex) => state, correlationId);
                logger.Log(timestamp.AddHours(1), Microsoft.Extensions.Logging.LogLevel.Error, 0, "Error message", null,
                           (state, ex) => state, correlationId);

                timestamp = timestamp.AddDays(1);
            }

            provider.IntervalControl.Resume();

            await provider.IntervalControl.Pause;

            System.String[] actualFiles = new System.IO.DirectoryInfo(this.TempPath).GetFiles().Select(f => f.Name).OrderBy(f => f).ToArray();

            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(6, actualFiles.Length);
            this.TestContext.WriteLine(actualFiles.ToJson());
            var expected = new[]
            {
                "LogFile.20160509.txt", "LogFile.20160510.txt", "LogFile.20160511.txt", "LogFile.20160512.txt",
                "LogFile.20160513.txt", "randomFile.txt"
            };

            this.TestContext.WriteLine(actualFiles.ToJson());
            this.TestContext.WriteLine($"{expected[0]}:{actualFiles[0]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[0], actualFiles[0]);
            this.TestContext.WriteLine($"{expected[1]}:{actualFiles[1]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[1], actualFiles[1]);
            this.TestContext.WriteLine($"{expected[2]}:{actualFiles[2]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[2], actualFiles[2]);
            this.TestContext.WriteLine($"{expected[3]}:{actualFiles[3]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[3], actualFiles[3]);
            this.TestContext.WriteLine($"{expected[4]}:{actualFiles[4]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[4], actualFiles[4]);
            this.TestContext.WriteLine($"{expected[5]}:{actualFiles[5]}");
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected[5], actualFiles[5]);
        }