From d8b3bbece9ee09b68b6707c276ce4101f91e2943 Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Sun, 12 Jan 2020 15:56:17 +0200 Subject: Add session log to TFS PPC and Machine Studio. Related Work Items: #2306 --- .../Tango.Logging/SessionFileLogger.cs | 46 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs') diff --git a/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs b/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs index f3c23ca25..f4ce9c2fb 100644 --- a/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs @@ -10,12 +10,22 @@ namespace Tango.Logging { public class SessionFileLogger : ILogger { + #region Properties private bool _inInSession; - public const string FILE_SESSION_EXTENSION = "_session"; + private int _writeCount; + public const string FILE_SESSION_EXTENSION = "_session"; + public static String DefaultLogsFolder { get; private set; } + private const int FILE_SIZE_CHECK_COUNT = 100; + + /// + /// Gets or sets the maximum file size limit. + /// + public static long MaxFileSizeLimit { get; set; } + public bool Enabled { get; set; } public String Folder { get; private set; } @@ -24,9 +34,15 @@ namespace Tango.Logging public String Tag { get; private set; } + #endregion + #region Constructors + /// + /// Initializes the static members of the class. + /// static SessionFileLogger() { - DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Logs", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName), "session"); + DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Logs", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName), "Session"); + MaxFileSizeLimit = 1000000 * 10; //10 MB } public SessionFileLogger(String folder, String tag) @@ -35,13 +51,17 @@ namespace Tango.Logging Tag = tag; Directory.CreateDirectory(Folder); Enabled = true; + _writeCount = 0; } public SessionFileLogger() : this(DefaultLogsFolder, Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)) { } - + #endregion + /// + /// Creates the session. Only one file should be for session log, so removed old file and create a new file. + /// public void CreateSession() { RemoveOldLogFile(); @@ -49,16 +69,26 @@ namespace Tango.Logging _inInSession = true; } + /// + /// Ends the session. Set flag _inInSession to false. + /// public void EndSession() { _inInSession = false; } + /// + /// Creates the name of the log file. + /// + /// private String CreateLogFileName() { return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}{2}.log", DateTime.Now, Tag, FILE_SESSION_EXTENSION)); } + /// + /// Delete the old log file if is existed. + /// private void RemoveOldLogFile() { try @@ -85,12 +115,22 @@ namespace Tango.Logging } } + /// + /// Called when write to session log file. + /// + /// The output. public void OnLog(LogItemBase output) { if (_inInSession) { try { + if(++_writeCount > FILE_SIZE_CHECK_COUNT && new FileInfo(LogFile).Length > MaxFileSizeLimit) + { + _writeCount = 0; + CreateSession(); + File.AppendAllText(LogFile, "### This log file is a continuation of a previous log file ###" + Environment.NewLine + Environment.NewLine); + } File.AppendAllText(LogFile, output.ToString() + Environment.NewLine); } catch -- cgit v1.3.1