aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging
diff options
context:
space:
mode:
authorVictoria Plitt <Victoria.Plitt@twine-s.com>2019-11-24 16:39:46 +0200
committerVictoria Plitt <Victoria.Plitt@twine-s.com>2019-11-24 16:39:46 +0200
commit6c57a826a4287b1ca3ea418fcc2aed50ed129bdc (patch)
tree76a3fc419861d2ebdb9e345a35e58cf37f3cde21 /Software/Visual_Studio/Tango.Logging
parentd2edfc56a8154c01a7ca9cfc47adccc8a07c3d94 (diff)
downloadTango-6c57a826a4287b1ca3ea418fcc2aed50ed129bdc.tar.gz
Tango-6c57a826a4287b1ca3ea418fcc2aed50ed129bdc.zip
Implemented AutoLogRemoval & MaxFileSizeLimit to FileLogger, LogFileParser and TFS bug reporting!!!
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging')
-rw-r--r--Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs54
-rw-r--r--Software/Visual_Studio/Tango.Logging/FileLogger.cs153
-rw-r--r--Software/Visual_Studio/Tango.Logging/LogFile.cs6
3 files changed, 196 insertions, 17 deletions
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<LogFile> logFiles = new List<LogFile>();
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<string> dateStrings = new HashSet<string>();
+ 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<LogItemBase> Parse(LogFile logFile)
{
List<LogItemBase> logItems = new List<LogItemBase>();
+ List<LogFile> logFiles = new List<LogFile>();
+ 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<LogItemBase> 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
/// <seealso cref="Tango.Logging.ILogger" />
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 = "__";
/// <summary>
/// Gets the logs folder.
@@ -37,6 +44,50 @@ namespace Tango.Logging
public String Folder { get; private set; }
/// <summary>
+ /// Gets or sets a value indicating whether [enable automatic log removal].
+ /// </summary>
+ public bool EnableAutoLogRemoval { get; set; }
+
+ /// <summary>
+ /// Gets or sets the automatic log removal period.
+ /// </summary>
+ public TimeSpan AutoLogRemovalPeriod { get; set; }
+
+ private TimeSpan _autoLogRemovalCheckPeriod;
+ /// <summary>
+ /// Gets or sets the automatic log removal check period.
+ /// </summary>
+ public TimeSpan AutoLogRemovalCheckPeriod
+ {
+ get { return _autoLogRemovalCheckPeriod; }
+ set
+ {
+ _autoLogRemovalCheckPeriod = value;
+
+ if (_removal_timer != null)
+ {
+ _removal_timer.Interval = _autoLogRemovalCheckPeriod.TotalMilliseconds;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether [enable maximum file size limit].
+ /// </summary>
+ public bool EnableMaxFileSizeLimit { get; set; }
+
+ /// <summary>
+ /// Gets or sets the maximum file size limit.
+ /// </summary>
+ public long MaxFileSizeLimit { get; set; }
+
+
+ public String GetFileSetExtension(int index)
+ {
+ return FILE_SET_EXTENSION + index;
+ }
+
+ /// <summary>
/// Initializes the <see cref="FileLogger"/> class.
/// </summary>
static FileLogger()
@@ -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
}
/// <summary>
@@ -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
/// <returns></returns>
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
+
+ /// <summary>
+ /// Handles the Elapsed event of the _removal_timer control.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
+ private void _removal_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+ {
+ if (EnableAutoLogRemoval)
+ {
+ RemoveOldLogFiles();
+ }
}
+
+ /// <summary>
+ /// Removes the old files.
+ /// </summary>
+ 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; }
}
}