aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/FileLogger.cs
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/FileLogger.cs
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/FileLogger.cs')
-rw-r--r--Software/Visual_Studio/Tango.Logging/FileLogger.cs153
1 files changed, 146 insertions, 7 deletions
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
}
}