diff options
| author | Victoria Plitt <Victoria.Plitt@twine-s.com> | 2019-11-24 16:39:46 +0200 |
|---|---|---|
| committer | Victoria Plitt <Victoria.Plitt@twine-s.com> | 2019-11-24 16:39:46 +0200 |
| commit | 6c57a826a4287b1ca3ea418fcc2aed50ed129bdc (patch) | |
| tree | 76a3fc419861d2ebdb9e345a35e58cf37f3cde21 /Software/Visual_Studio | |
| parent | d2edfc56a8154c01a7ca9cfc47adccc8a07c3d94 (diff) | |
| download | Tango-6c57a826a4287b1ca3ea418fcc2aed50ed129bdc.tar.gz Tango-6c57a826a4287b1ca3ea418fcc2aed50ed129bdc.zip | |
Implemented AutoLogRemoval & MaxFileSizeLimit to FileLogger, LogFileParser and TFS bug reporting!!!
Diffstat (limited to 'Software/Visual_Studio')
10 files changed, 364 insertions, 65 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs index 8309500b7..94f98feb2 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs @@ -23,13 +23,14 @@ using Tango.MachineStudio.UI.Views; using Tango.MachineStudio.Common; using Tango.Core; using Tango.BL; +using Tango.Integration.Operation; namespace Tango.MachineStudio.UI { /// <summary> /// Interaction logic for App.xaml /// </summary> - public partial class App : Application + public partial class App : Application { private WpfGlobalExceptionTrapper exceptionTrapper; private LogManager LogManager = LogManager.Default; @@ -61,7 +62,14 @@ namespace Tango.MachineStudio.UI #if DEBUG LogManager.RegisterLogger(new VSOutputLogger()); #endif - LogManager.RegisterLogger(new FileLogger()); + LogManager.RegisterLogger(new FileLogger() { EnableAutoLogRemoval = true, EnableMaxFileSizeLimit = true }); + + var operatorLogger = MachineOperator.EmbeddedLogManager.RegisteredLoggers.SingleOrDefault(x => x is FileLogger) as FileLogger; + if (operatorLogger != null) + { + operatorLogger.EnableAutoLogRemoval = true; + operatorLogger.EnableMaxFileSizeLimit = true; + } LogManager.Log("Application Started..."); @@ -89,7 +97,13 @@ namespace Tango.MachineStudio.UI exceptionTrapper.Initialize(this); exceptionTrapper.ApplicationCrashed += ExceptionTrapper_ApplicationCrashed; - //Apply Caching + ApplyEFCacheSettings(); + } + + private void ApplyEFCacheSettings() + { + var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); + if (settings.CachingMode != ObservablesContextInMemoryCachingMode.None) { LogManager.Log("EF Caching is enabled."); @@ -108,9 +122,7 @@ namespace Tango.MachineStudio.UI { LogManager.Log("EF Caching is disabled"); } - } - #region Global Exception Trapping diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/TFS/TeamFoundationServiceExtendedClient.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/TFS/TeamFoundationServiceExtendedClient.cs index 31b330c3f..b3e121253 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/TFS/TeamFoundationServiceExtendedClient.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/TFS/TeamFoundationServiceExtendedClient.cs @@ -185,6 +185,22 @@ namespace Tango.MachineStudio.UI.TFS return item; } + private string[] GetLogFiles(FileLogger logger) + { + string[] fileEntries = new string[1]; + fileEntries[0] = logger.LogFile; + string fileName = Path.GetFileNameWithoutExtension(logger.LogFile); + int indexPos = fileName.IndexOf(FileLogger.FILE_SET_EXTENSION); + if (indexPos > 0) + { + string extension = Path.GetExtension(logger.LogFile); + fileName = fileName.Substring(0, indexPos); + fileEntries = Directory.GetFiles(logger.Folder, $"{fileName}*{extension}").Where(x => Path.GetFileName(x).StartsWith(logger.Tag)).OrderBy(x => x.Length).ThenBy(x => x).ToArray(); + } + + return fileEntries; + } + public void FinalizeBug(WorkItem item) { IAuthenticationProvider auth = TangoIOC.Default.GetInstance<IAuthenticationProvider>(); @@ -192,31 +208,37 @@ namespace Tango.MachineStudio.UI.TFS FileLogger appFileLogger = LogManager.Default.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; FileLogger embeddedFileLogger = MachineOperator.EmbeddedLogManager.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; - + if (appFileLogger != null) { - var appLogFile = _tempFolder.CreateImaginaryFile(); - File.Copy(appFileLogger.LogFile, appLogFile.Path); - - item.Attachments.Add(new Attachment() + string[] logFiles = GetLogFiles(appFileLogger); + foreach( string file in logFiles) { - Description = "Application Log File", - FilePath = appLogFile.Path, - Name = Path.GetFileName(appFileLogger.LogFile), - }); + var appLogFile = _tempFolder.CreateImaginaryFile(); + File.Copy(file, appLogFile.Path); + item.Attachments.Add(new Attachment() + { + Description = "Application Log File", + FilePath = appLogFile.Path, + Name = Path.GetFileName(file), + }); + } } if (embeddedFileLogger != null && File.Exists(embeddedFileLogger.LogFile)) { - var embeddedLogFile = _tempFolder.CreateImaginaryFile(); - File.Copy(embeddedFileLogger.LogFile, embeddedLogFile.Path); - - item.Attachments.Add(new Attachment() + string[] logFiles = GetLogFiles(embeddedFileLogger); + foreach (string file in logFiles) { - Description = "Embedded Log File", - FilePath = embeddedLogFile.Path, - Name = Path.GetFileName(embeddedFileLogger.LogFile), - }); + var embeddedLogFile = _tempFolder.CreateImaginaryFile(); + File.Copy(file, embeddedLogFile.Path); + item.Attachments.Add(new Attachment() + { + Description = "Embedded Log File", + FilePath = embeddedLogFile.Path, + Name = Path.GetFileName(file), + }); + } } SystemInformationModel sysModel = new SystemInformationModel(); diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.BugReporting/TFS/TeamFoundationServicePPCClient.cs b/Software/Visual_Studio/PPC/Modules/Tango.PPC.BugReporting/TFS/TeamFoundationServicePPCClient.cs index 26d6425bf..c28c5fcba 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.BugReporting/TFS/TeamFoundationServicePPCClient.cs +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.BugReporting/TFS/TeamFoundationServicePPCClient.cs @@ -53,6 +53,21 @@ namespace Tango.PPC.BugReporting.TFS } } + private string[] GetLogFiles(FileLogger logger) + { + string[] fileEntries = new string[1]; + fileEntries[0] = logger.LogFile; + string fileName = Path.GetFileNameWithoutExtension(logger.LogFile); + int indexPos = fileName.IndexOf(FileLogger.FILE_SET_EXTENSION); + if (indexPos > 0) + { + string extension = Path.GetExtension(logger.LogFile); + fileName = fileName.Substring(0, indexPos); + fileEntries = Directory.GetFiles(logger.Folder, $"{fileName}*{extension}").Where(x => Path.GetFileName(x).StartsWith(logger.Tag)).OrderBy(x => x.Length).ThenBy(x => x).ToArray(); + } + return fileEntries; + } + public async Task SubmitBug(String title, String steps, TeamMember createdBy, TeamMember assignedTo, Severity severity) { LogManager.Log("Submitting bug report..."); @@ -87,31 +102,36 @@ namespace Tango.PPC.BugReporting.TFS if (appFileLogger != null) { LogManager.Log($"Attaching application log file ${appFileLogger.LogFile}"); - - var appLogFile = tempFolder.CreateImaginaryFile(); - File.Copy(appFileLogger.LogFile, appLogFile.Path); - - item.Attachments.Add(new Attachment() + string[] logFiles = GetLogFiles(appFileLogger); + foreach (string file in logFiles) { - Description = "Application Log File", - FilePath = appLogFile.Path, - Name = Path.GetFileName(appFileLogger.LogFile), - }); + var appLogFile = tempFolder.CreateImaginaryFile(); + File.Copy(file, appLogFile.Path); + item.Attachments.Add(new Attachment() + { + Description = "Application Log File", + FilePath = appLogFile.Path, + Name = Path.GetFileName(file), + }); + } } if (embeddedFileLogger != null && File.Exists(embeddedFileLogger.LogFile)) { LogManager.Log($"Attaching embedded log file ${embeddedFileLogger.LogFile}"); - var embeddedLogFile = tempFolder.CreateImaginaryFile(); - File.Copy(embeddedFileLogger.LogFile, embeddedLogFile.Path); - - item.Attachments.Add(new Attachment() + string[] logFiles = GetLogFiles(embeddedFileLogger); + foreach (string file in logFiles) { - Description = "Embedded Log File", - FilePath = embeddedLogFile.Path, - Name = Path.GetFileName(embeddedFileLogger.LogFile), - }); + var embeddedLogFile = tempFolder.CreateImaginaryFile(); + File.Copy(file, embeddedLogFile.Path); + item.Attachments.Add(new Attachment() + { + Description = "Embedded Log File", + FilePath = embeddedLogFile.Path, + Name = Path.GetFileName(file), + }); + } } SystemInformationModel sysModel = new SystemInformationModel(); diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/Controls/JobOutlineControl.cs b/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/Controls/JobOutlineControl.cs index 8d57df139..e6f090fdd 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/Controls/JobOutlineControl.cs +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/Controls/JobOutlineControl.cs @@ -68,7 +68,7 @@ namespace Tango.PPC.Jobs #region events private void JobOutlineControl_Unloaded(object sender, RoutedEventArgs e) { - if (_parentScrollViewer == null) + if (_parentScrollViewer != null) { _parentScrollViewer.ScrollChanged -= ScrollViewer_ScrollChanged; } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs index 0bd9f9d1d..bd3f04958 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs @@ -11,6 +11,7 @@ using Tango.BL; using Tango.Core; using Tango.Core.DI; using Tango.Core.Helpers; +using Tango.Integration.Operation; using Tango.Logging; using Tango.PPC.Common; using Tango.PPC.Common.EventLogging; @@ -45,9 +46,17 @@ namespace Tango.PPC.UI StartupArgs = e.Args; //LogManager.RegisterLogger(new ConsoleLogger("Tango PPC Debug")); - LogManager.RegisterLogger(new FileLogger()); + LogManager.RegisterLogger(new FileLogger() { EnableAutoLogRemoval = true, EnableMaxFileSizeLimit = true }); LogManager.RegisterLogger(new VSOutputLogger()); + //Configure machine operator logger. + var operatorLogger = MachineOperator.EmbeddedLogManager.RegisteredLoggers.SingleOrDefault(x => x is FileLogger) as FileLogger; + if (operatorLogger != null) + { + operatorLogger.EnableAutoLogRemoval = true; + operatorLogger.EnableMaxFileSizeLimit = true; + } + LogManager.Log("Application Started..."); base.OnStartup(e); diff --git a/Software/Visual_Studio/Tango.Integration/Logging/EmbeddedLogFileParser.cs b/Software/Visual_Studio/Tango.Integration/Logging/EmbeddedLogFileParser.cs index 98a3ac543..72e55bbfd 100644 --- a/Software/Visual_Studio/Tango.Integration/Logging/EmbeddedLogFileParser.cs +++ b/Software/Visual_Studio/Tango.Integration/Logging/EmbeddedLogFileParser.cs @@ -21,7 +21,7 @@ namespace Tango.Integration.Logging FileLogger logger = MachineOperator.EmbeddedLogManager.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; String logFile = logger != null ? logger.LogFile : null; - + HashSet<string> dateStrings = new HashSet<string>(); if (Directory.Exists(MachineOperator.EmbeddedLogsFolder)) { foreach (var file in Directory.GetFiles(MachineOperator.EmbeddedLogsFolder, "*.log").Where(x => x != logFile)) @@ -29,8 +29,20 @@ namespace Tango.Integration.Logging try { String dateString = Path.GetFileNameWithoutExtension(file).Replace(MachineOperator.EmbeddedLogsTag + "-", ""); - 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) { @@ -45,8 +57,33 @@ namespace Tango.Integration.Logging public List<EmbeddedLogItem> Parse(LogFile logFile) { List<EmbeddedLogItem> logItems = new List<EmbeddedLogItem>(); + 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; + } + public void Parse(string file, DateTime datetime, ref List<EmbeddedLogItem> 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) @@ -68,7 +105,7 @@ namespace Tango.Integration.Logging Message = new String(entries[10].Skip(2).ToArray()) }); - 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); logItems.Add(item); } @@ -77,8 +114,6 @@ namespace Tango.Integration.Logging LogManager.Default.Log(ex, "Could not parse log line: " + logs[i]); } } - - return logItems; } } } 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; } } } diff --git a/Software/Visual_Studio/Tango.UnitTesting/Logging/Parsing_TST.cs b/Software/Visual_Studio/Tango.UnitTesting/Logging/Parsing_TST.cs index 57856f9cb..8cc87e700 100644 --- a/Software/Visual_Studio/Tango.UnitTesting/Logging/Parsing_TST.cs +++ b/Software/Visual_Studio/Tango.UnitTesting/Logging/Parsing_TST.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using Tango.Integration.Logging; using Tango.Logging; @@ -30,5 +31,26 @@ namespace Tango.UnitTesting.Logging var logFile = logFiles.OrderByDescending(x => x.DateTime).First(); var logs = parser.Parse(logFile); } + + [TestMethod] + public void Parse_Multi_Part_Log_Files() + { + var manager = LogManager.Default; + var fileLogger = new FileLogger(); + var folder = FileLogger.DefaultLogsFolder; + fileLogger.EnableMaxFileSizeLimit = true; + fileLogger.MaxFileSizeLimit = 10000; + LogManager.Default.RegisterLogger(fileLogger); + for (int i = 0; i < 100; i++) + { + manager.Log($"This is a test {i}"); + Thread.Sleep(100); + } + Thread.Sleep(1000); + ApplicationLogFileParser parser = new ApplicationLogFileParser(); + var logFiles = parser.GetLogFiles(); + var logFile = logFiles.OrderByDescending(x => x.DateTime).First(); + var logs = parser.Parse(logFile); + } } } |
