From 6c57a826a4287b1ca3ea418fcc2aed50ed129bdc Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Sun, 24 Nov 2019 16:39:46 +0200 Subject: Implemented AutoLogRemoval & MaxFileSizeLimit to FileLogger, LogFileParser and TFS bug reporting!!! --- .../Tango.Logging/ApplicationLogFileParser.cs | 54 ++++++-- Software/Visual_Studio/Tango.Logging/FileLogger.cs | 153 ++++++++++++++++++++- Software/Visual_Studio/Tango.Logging/LogFile.cs | 6 + 3 files changed, 196 insertions(+), 17 deletions(-) (limited to 'Software/Visual_Studio/Tango.Logging') diff --git a/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs b/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs index 2870ce95d..e91734ada 100644 --- a/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs +++ b/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs @@ -17,16 +17,26 @@ namespace Tango.Logging List logFiles = new List(); FileLogger logger = LogManager.Default.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; - - String logFile = logger != null ? logger.LogFile : null; - - foreach (var file in Directory.GetFiles(FileLogger.DefaultLogsFolder, "*.log").Where(x => Path.GetFileName(x).StartsWith(logger.Tag) && x != logger.LogFile)) + HashSet dateStrings = new HashSet(); + foreach (var file in Directory.GetFiles(FileLogger.DefaultLogsFolder, "*.log").Where(x => (Path.GetFileName(x).StartsWith(logger.Tag) && x != logger.LogFile))) { try { String dateString = Path.GetFileNameWithoutExtension(file).Replace($"{logger.Tag}-", ""); - DateTime date = DateTime.ParseExact(dateString, "dd-MM-yyyy_HH-mm-ss", CultureInfo.InvariantCulture); - logFiles.Add(new LogFile() { DateTime = date, File = file }); + int indexPos = dateString.IndexOf(FileLogger.FILE_SET_EXTENSION); + int indexOfFile = 0; + if (indexPos > 0) + { + string fileNameIndex = dateString.Substring(indexPos + FileLogger.FILE_SET_EXTENSION.Length); + int.TryParse(fileNameIndex, out indexOfFile); + dateString = dateString.Substring(0, indexPos); + } + if (!dateStrings.Contains(dateString)) + { + dateStrings.Add(dateString); + DateTime date = DateTime.ParseExact(dateString, "dd-MM-yyyy_HH-mm-ss", CultureInfo.InvariantCulture); + logFiles.Add(new LogFile() { DateTime = date, File = file, PartOfSet = indexOfFile > 0, }); + } } catch (Exception ex) { @@ -40,8 +50,34 @@ namespace Tango.Logging public List Parse(LogFile logFile) { List logItems = new List(); + List logFiles = new List(); + FileLogger logger = LogManager.Default.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; + if (logFile.PartOfSet) + { + string fileName = Path.GetFileNameWithoutExtension(logFile.File); + string extension = Path.GetExtension(logFile.File); + int indexPos = fileName.IndexOf(FileLogger.FILE_SET_EXTENSION); + if (indexPos > 0) + { + fileName = fileName.Substring(0, indexPos); + } + string[] fileEntries = Directory.GetFiles(FileLogger.DefaultLogsFolder, $"{fileName}*{extension}").Where(x => Path.GetFileName(x).StartsWith(logger.Tag) && x != logger.LogFile).OrderBy(x => x).ToArray(); + foreach (var file in fileEntries) + { + Parse(file, logFile.DateTime, ref logItems); + } + } + else + { + Parse(logFile.File, logFile.DateTime, ref logItems); + } - String text = File.ReadAllText(logFile.File); + return logItems; + } + + private void Parse(string file, DateTime datetime, ref List logItems) + { + String text = File.ReadAllText(file); var logs = Regex.Split(text, @"(\[\d{2}:\d{2}:\d{2}.\d{2}\])"); for (int i = 1; i < logs.Length; i += 2) @@ -54,7 +90,7 @@ namespace Tango.Logging var entries = Regex.Split(rest, @"\[(.*?)\]"); MessageLogItem item = new MessageLogItem(); - item.TimeStamp = new DateTime(logFile.DateTime.Year, logFile.DateTime.Month, logFile.DateTime.Day, date.Hour, date.Minute, date.Second, date.Millisecond); + item.TimeStamp = new DateTime(datetime.Year, datetime.Month, datetime.Day, date.Hour, date.Minute, date.Second, date.Millisecond); item.Category = (LogCategory)Enum.Parse(typeof(LogCategory), entries[1]); item.CallerFile = entries[3]; item.CallerMethodName = entries[5]; @@ -68,8 +104,6 @@ namespace Tango.Logging LogManager.Default.Log(ex, "Could not parse log line: " + logs[i]); } } - - return logItems; } } } diff --git a/Software/Visual_Studio/Tango.Logging/FileLogger.cs b/Software/Visual_Studio/Tango.Logging/FileLogger.cs index 121ef5374..3b911b4d4 100644 --- a/Software/Visual_Studio/Tango.Logging/FileLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/FileLogger.cs @@ -4,7 +4,9 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; +using System.Windows.Threading; namespace Tango.Logging { @@ -14,7 +16,12 @@ namespace Tango.Logging /// public class FileLogger : ILogger { - private DateTime _logFileDate; + private DateTime _logFileTimeDate; + private System.Timers.Timer _removal_timer; + private int _writeCount; + private int _fileExtensionIndex; + private const int FILE_SIZE_CHECK_COUNT = 100; + public const string FILE_SET_EXTENSION = "__"; /// /// Gets the logs folder. @@ -36,6 +43,50 @@ namespace Tango.Logging /// public String Folder { get; private set; } + /// + /// Gets or sets a value indicating whether [enable automatic log removal]. + /// + public bool EnableAutoLogRemoval { get; set; } + + /// + /// Gets or sets the automatic log removal period. + /// + public TimeSpan AutoLogRemovalPeriod { get; set; } + + private TimeSpan _autoLogRemovalCheckPeriod; + /// + /// Gets or sets the automatic log removal check period. + /// + public TimeSpan AutoLogRemovalCheckPeriod + { + get { return _autoLogRemovalCheckPeriod; } + set + { + _autoLogRemovalCheckPeriod = value; + + if (_removal_timer != null) + { + _removal_timer.Interval = _autoLogRemovalCheckPeriod.TotalMilliseconds; + } + } + } + + /// + /// Gets or sets a value indicating whether [enable maximum file size limit]. + /// + public bool EnableMaxFileSizeLimit { get; set; } + + /// + /// Gets or sets the maximum file size limit. + /// + public long MaxFileSizeLimit { get; set; } + + + public String GetFileSetExtension(int index) + { + return FILE_SET_EXTENSION + index; + } + /// /// Initializes the class. /// @@ -54,7 +105,20 @@ namespace Tango.Logging Tag = Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName); Folder = DefaultLogsFolder; Directory.CreateDirectory(Folder); + _logFileTimeDate = DateTime.Now; LogFile = CreateLogFileName(); + + EnableAutoLogRemoval = false; + AutoLogRemovalCheckPeriod = TimeSpan.FromHours(1); + AutoLogRemovalPeriod = TimeSpan.FromDays(7); + + _removal_timer = new System.Timers.Timer(); + _removal_timer.Interval = AutoLogRemovalCheckPeriod.TotalMilliseconds; + _removal_timer.Elapsed += _removal_timer_Elapsed; + _removal_timer.Start(); + + EnableMaxFileSizeLimit = false; + MaxFileSizeLimit = 1000000 * 10; //10 MB } /// @@ -79,11 +143,28 @@ namespace Tango.Logging { try { - if (DateTime.Now.Date > _logFileDate.Date) + if (DateTime.Now.Date > _logFileTimeDate.Date) + { + _fileExtensionIndex = 0; + _writeCount = 0; + _logFileTimeDate = DateTime.Now; + CreateNewLogFile(); + } + else if (EnableMaxFileSizeLimit && ++_writeCount > FILE_SIZE_CHECK_COUNT) { - File.AppendAllText(LogFile, Environment.NewLine + Environment.NewLine + "### This log file continues on the next log file ###" + Environment.NewLine); - LogFile = CreateLogFileName(); - File.AppendAllText(LogFile, "### This log file is a continuation of a previous log file ###" + Environment.NewLine + Environment.NewLine); + if (new FileInfo(LogFile).Length > MaxFileSizeLimit) + { + if (_fileExtensionIndex == 0) + { + _fileExtensionIndex = 1; + string oldPath = LogFile; + LogFile = CreateLogFileName(); + File.Move(oldPath, LogFile); + } + _fileExtensionIndex++; + CreateNewLogFile(); + _writeCount = 0; + } } File.AppendAllText(LogFile, output.ToString() + Environment.NewLine); @@ -116,8 +197,66 @@ namespace Tango.Logging /// private String CreateLogFileName() { - _logFileDate = DateTime.Now.Date; - return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}.log", DateTime.Now, Tag)); + return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}{2}.log", _logFileTimeDate, Tag, EnableMaxFileSizeLimit && _fileExtensionIndex > 0 ? GetFileSetExtension(_fileExtensionIndex) : String.Empty)); + } + + private void CreateNewLogFile() + { + File.AppendAllText(LogFile, Environment.NewLine + Environment.NewLine + "### This log file continues on the next log file ###" + Environment.NewLine); + LogFile = CreateLogFileName(); + File.AppendAllText(LogFile, "### This log file is a continuation of a previous log file ###" + Environment.NewLine + Environment.NewLine); + } + + #region Auto Log Removal + + /// + /// Handles the Elapsed event of the _removal_timer control. + /// + /// The source of the event. + /// The instance containing the event data. + private void _removal_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + if (EnableAutoLogRemoval) + { + RemoveOldLogFiles(); + } } + + /// + /// Removes the old files. + /// + public void RemoveOldLogFiles() + { + try + { + if (Directory.Exists(Folder)) + { + DateTime removalDateTime = DateTime.Now - AutoLogRemovalPeriod; + string[] fileEntries = Directory.GetFiles(Folder, "*.log"); + foreach (string fileName in fileEntries) + { + try + { + FileInfo fi = new FileInfo(fileName); + + if (fi != null && fi.LastWriteTime < removalDateTime) + { + File.Delete(fi.FullName); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } + } + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } + + #endregion } } diff --git a/Software/Visual_Studio/Tango.Logging/LogFile.cs b/Software/Visual_Studio/Tango.Logging/LogFile.cs index 66988b7ed..24a8a11f2 100644 --- a/Software/Visual_Studio/Tango.Logging/LogFile.cs +++ b/Software/Visual_Studio/Tango.Logging/LogFile.cs @@ -11,5 +11,11 @@ namespace Tango.Logging public DateTime DateTime { get; set; } public String File { get; set; } + + public bool PartOfSet { get; set; } + + public int SetStartIndex { get; set; } + + public int SetCount { get; set; } } } -- cgit v1.3.1