Пример #1
0
    public void LogRotatorFileCountDetection()
    {
        ClearTestLoggingPath();
        var config = new TextLogOutputConfig {
            // we're generating 15 files from 1900 - thus the 15th file is 1914
            LoggingPath       = _folderPath + "/log 1994-01-01T00-00-00.txt",
            LogRotate         = true,
            LogRotateMaxFiles = 10
        };
        var rotator = new LogRotator(config);

        for (var i = 0; i < 15; i++)
        {
            var filePath = _folderPath + string.Format(_filenameFormat, new DateTime(1980 + i, 1, 1));
            var fs       = File.Create(filePath);
            fs.Close();
            // File creation dates are only per seconds, so we're skipping a bit on time (giving us a future).
            File.SetCreationTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastAccessTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastWriteTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
        }

        rotator.RefreshObservedFiles();
        rotator.PerformLogCountTrimming();


        var files = Directory.GetFiles(_folderPath);

        Assert.AreEqual(10, files.Length);
        Assert.IsTrue(files.Contains(config.LoggingPath));
    }
Пример #2
0
    public void LogRotatorFileAgeDetection()
    {
        ClearTestLoggingPath();
        var config = new TextLogOutputConfig {
            LoggingPath = _folderPath + "/log 1994-01-01T00-00-00.txt",
            LogRotate   = true,
            // a year.
            LogRotateMaxAge = new TimeSpan(365, 0, 0, 0)
        };
        var rotator = new LogRotator(config);

        for (var i = 0; i < 15; i++)
        {
            var filePath = _folderPath + string.Format(_filenameFormat, new DateTime(1980 + i, 1, 1));
            var fs       = File.Create(filePath);
            fs.Close();
            File.SetCreationTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastAccessTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastWriteTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
        }
        File.SetCreationTimeUtc(config.LoggingPath, DateTime.UtcNow);
        File.SetLastAccessTimeUtc(config.LoggingPath, DateTime.UtcNow);
        File.SetLastWriteTimeUtc(config.LoggingPath, DateTime.UtcNow);
        rotator.RefreshObservedFiles();
        rotator.PerformLogTimeSpanTrimming();
        var files = Directory.GetFiles(_folderPath);

        Assert.AreEqual(1, files.Length);
        Assert.AreEqual(config.LoggingPath, files[0]);
    }
Пример #3
0
    public void LogRotatorFileSizeDetection()
    {
        ClearTestLoggingPath();
        var config = new TextLogOutputConfig {
            LoggingPath      = _folderPath + "/log 1994-01-01T00-00-00.txt",
            LogRotate        = true,
            LogRotateMaxSize = 5 * 1024
        };
        var rotator = new LogRotator(config);

        for (var i = 0; i < 15; i++)
        {
            var filePath = _folderPath + string.Format(_filenameFormat, new DateTime(1980 + i, 1, 1));
            var fs       = File.Create(filePath);
            // dump 1024 bytes into the file, so we should have 15kbyte of actual log data,
            // disregarding drive chunk size
            for (var j = 0; j < 1024; j++)
            {
                fs.WriteByte(0x00);
            }
            fs.Flush();
            fs.Close();
            File.SetCreationTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastAccessTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
            File.SetLastWriteTimeUtc(filePath, new DateTime(1980 + i, 1, 1));
        }
        rotator.RefreshObservedFiles();
        rotator.PerformLogSizeTrimming();
        var files = Directory.GetFiles(_folderPath);

        Assert.AreEqual(5, files.Length);
        Assert.IsTrue(files.Contains(config.LoggingPath));
    }