Exemple #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));
    }
Exemple #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]);
    }
Exemple #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));
    }
Exemple #4
0
        /// <summary>
        /// return int position - current position
        /// return string[] lines - lines from current log file
        /// </summary>
        /// <param name="lines">max lines to return</param>
        /// <param name="position">position to seek</param>
        /// <returns></returns>
        private object GetLog(int lines, int position)
        {
            string log_file = LogRotator.GetCurrentLogFile();

            if (string.IsNullOrEmpty(log_file))
            {
                return(APIStatus.notFound404("Could not find current log name. Sorry"));
            }

            if (!File.Exists(log_file))
            {
                return(APIStatus.notFound404());
            }

            Dictionary <string, object> result = new Dictionary <string, object>();
            FileStream fs = File.OpenRead(@log_file);

            if (position >= fs.Length)
            {
                result.Add("position", fs.Length);
                result.Add("lines", new string[] { });
                return(result);
            }

            List <string> logLines = new List <string>();

            LogReader reader = new LogReader(fs, position);

            for (int i = 0; i < lines; i++)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                logLines.Add(line);
            }
            result.Add("position", reader.Position);
            result.Add("lines", logLines.ToArray());
            return(result);
        }