aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/FileLogger.cs
diff options
context:
space:
mode:
authorMirta <mirta@twine-s.com>2020-12-30 16:39:52 +0200
committerMirta <mirta@twine-s.com>2020-12-30 16:39:52 +0200
commit00a491d93733d4625ad329b2ba8237f445364b3f (patch)
tree4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.Logging/FileLogger.cs
parent124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff)
downloadTango-00a491d9.tar.gz
Tango-00a491d9.zip
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging/FileLogger.cs')
-rw-r--r--Software/Visual_Studio/Tango.Logging/FileLogger.cs193
1 files changed, 19 insertions, 174 deletions
diff --git a/Software/Visual_Studio/Tango.Logging/FileLogger.cs b/Software/Visual_Studio/Tango.Logging/FileLogger.cs
index 3c3ae970d..121ef5374 100644
--- a/Software/Visual_Studio/Tango.Logging/FileLogger.cs
+++ b/Software/Visual_Studio/Tango.Logging/FileLogger.cs
@@ -4,10 +4,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
-using System.Text.RegularExpressions;
-using System.Threading;
using System.Threading.Tasks;
-using System.Windows.Threading;
namespace Tango.Logging
{
@@ -17,18 +14,7 @@ namespace Tango.Logging
/// <seealso cref="Tango.Logging.ILogger" />
public class FileLogger : ILogger
{
- 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 = "__";
- private Regex _passwordRegEx;
-
- /// <summary>
- /// Gets or sets a value indicating whether to automatically locate password inside log messages and hide them.
- /// </summary>
- public bool ProtectPasswords { get; set; }
+ private DateTime _logFileDate;
/// <summary>
/// Gets the logs folder.
@@ -51,49 +37,6 @@ 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()
@@ -104,40 +47,28 @@ namespace Tango.Logging
/// <summary>
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
- /// <param name="folder">Logs folder path.</param>
- /// <param name="tag">The tag name which will be appended to the file name.</param>
- public FileLogger(String folder, String tag)
+ public FileLogger()
{
_isEnabled = true;
- Tag = tag;
- Folder = folder;
+ 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 = true;
- MaxFileSizeLimit = 1000000 * 10;
- _fileExtensionIndex = 0;
-
- _passwordRegEx = new Regex("(\"(|.+)password\":)(|\\s)(\"[^,\"]+\")", RegexOptions.IgnoreCase);
}
/// <summary>
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
- public FileLogger() : this(DefaultLogsFolder, Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName))
+ /// <param name="folder">Logs folder path.</param>
+ /// <param name="tag">The tag name which will be appended to the file name.</param>
+ public FileLogger(String folder, String tag)
+ : this()
{
-
+ Folder = folder;
+ Tag = tag;
+ Directory.CreateDirectory(Folder);
+ LogFile = CreateLogFileName();
}
/// <summary>
@@ -148,42 +79,14 @@ namespace Tango.Logging
{
try
{
- if (DateTime.Now.Date > _logFileTimeDate.Date)
- {
- _fileExtensionIndex = 0;
- _writeCount = 0;
- _logFileTimeDate = DateTime.Now;
- CreateNewLogFile();
- }
- else if (EnableMaxFileSizeLimit && ++_writeCount > FILE_SIZE_CHECK_COUNT)
- {
- if (new FileInfo(LogFile).Length > MaxFileSizeLimit)
- {
- if (_fileExtensionIndex == 0)
- {
- _fileExtensionIndex = 1;
- string oldPath = LogFile;
- LogFile = CreateLogFileName();
- File.Move(oldPath, LogFile);
- }
- _fileExtensionIndex++;
- CreateNewLogFile();
- _writeCount = 0;
- }
- }
-
- String logString = output.ToString();
-
- if (ProtectPasswords)
+ if (DateTime.Now.Date > _logFileDate.Date)
{
- try
- {
- logString = _passwordRegEx.Replace(logString, x => $"{x.Groups[1].Value}{x.Groups[3].Value}\"{new String('*', x.Groups[4].Length)}\"");
- }
- catch { }
+ 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);
}
- File.AppendAllText(LogFile, logString + Environment.NewLine);
+ File.AppendAllText(LogFile, output.ToString() + Environment.NewLine);
}
catch
{
@@ -213,66 +116,8 @@ namespace Tango.Logging
/// <returns></returns>
private String CreateLogFileName()
{
- 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);
+ _logFileDate = DateTime.Now.Date;
+ return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}.log", DateTime.Now, Tag));
}
-
- #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
}
}