From ca293b80c52a54c73251fbf3cd50741fb5653ae9 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 15 Apr 2018 19:51:07 +0300 Subject: Lots Of Work ! --- .../Tango.Logging/ExceptionLogItem.cs | 11 +++++++---- Software/Visual_Studio/Tango.Logging/FileLogger.cs | 17 ++++++++++++++--- .../Visual_Studio/Tango.Logging/LogItemBase.cs | 22 ++++++++++++++++++++-- Software/Visual_Studio/Tango.Logging/LogManager.cs | 3 +++ .../Visual_Studio/Tango.Logging/MessageLogItem.cs | 20 +++++++++++--------- 5 files changed, 55 insertions(+), 18 deletions(-) (limited to 'Software/Visual_Studio/Tango.Logging') diff --git a/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs b/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs index cc8980cdc..de8485249 100644 --- a/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs +++ b/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs @@ -25,10 +25,13 @@ namespace Tango.Logging /// /// Gets the log message. /// - /// - public override string GetMessage() + public override string Message { - return Exception.Message; + get + { + return Exception.Message; + } + set { } } /// @@ -36,7 +39,7 @@ namespace Tango.Logging /// public override string ToString() { - return String.Format("[{0}] [{6}] [{1}] [{2}] [Line {3}]: {4}{5}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Description, Environment.NewLine + Exception.FlattenException(), Category); + return String.Format("[{0}] [{6}] [{1}] [{2}] [{3}]: {4}{5}", TimeStamp.ToString("HH:mm:ss.ff"), GetRelativeCallerFilePath(), CallerMethodName, CallerLineNumber, Description, Environment.NewLine + Exception.FlattenException(), Category); } } diff --git a/Software/Visual_Studio/Tango.Logging/FileLogger.cs b/Software/Visual_Studio/Tango.Logging/FileLogger.cs index e104b59b9..215b1d9ac 100644 --- a/Software/Visual_Studio/Tango.Logging/FileLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/FileLogger.cs @@ -13,21 +13,32 @@ namespace Tango.Logging /// public class FileLogger : ILogger { + /// + /// Gets the logs folder. + /// + public static String DefaultLogsFolder { get; private set; } /// /// Gets or sets the log file. /// public String LogFile { get; private set; } + /// + /// Initializes the class. + /// + static FileLogger() + { + DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs"); + } + /// /// Initializes a new instance of the class. /// public FileLogger() { _isEnabled = true; - String logsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs"); - Directory.CreateDirectory(logsFolder); - LogFile = Path.Combine(logsFolder, string.Format("{1}-{0:dd-MM-yyyy_hh-mm-ss}.log", DateTime.Now, Path.GetFileNameWithoutExtension(System.AppDomain.CurrentDomain.FriendlyName))); + Directory.CreateDirectory(DefaultLogsFolder); + LogFile = Path.Combine(DefaultLogsFolder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}.log", DateTime.Now, Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName))); } /// diff --git a/Software/Visual_Studio/Tango.Logging/LogItemBase.cs b/Software/Visual_Studio/Tango.Logging/LogItemBase.cs index 85451488a..6b282b6ab 100644 --- a/Software/Visual_Studio/Tango.Logging/LogItemBase.cs +++ b/Software/Visual_Studio/Tango.Logging/LogItemBase.cs @@ -11,6 +11,13 @@ namespace Tango.Logging /// public abstract class LogItemBase { + private static String base_path; + + /// + /// Gets or sets the assembly. + /// + public String CallerAssembly { get; set; } + /// /// Gets or sets the caller method adding the exception. /// @@ -39,12 +46,23 @@ namespace Tango.Logging /// /// Gets the log message. /// - /// - public abstract String GetMessage(); + public abstract String Message { get; set; } /// /// Returns a formatted string of the log item. /// public abstract override String ToString(); + + protected String GetRelativeCallerFilePath() + { + if (base_path == null) + { + int index = CallerFile.IndexOf("Visual_Studio") + "Visual_Studio".Length + 1; + String relative = CallerFile.Substring(index, CallerFile.Length - index); + base_path = CallerFile.Replace(relative, ""); + } + + return CallerFile.Replace(base_path, ""); + } } } diff --git a/Software/Visual_Studio/Tango.Logging/LogManager.cs b/Software/Visual_Studio/Tango.Logging/LogManager.cs index c654b646f..804fc14c5 100644 --- a/Software/Visual_Studio/Tango.Logging/LogManager.cs +++ b/Software/Visual_Studio/Tango.Logging/LogManager.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Threading; @@ -107,6 +108,7 @@ namespace Tango.Logging if (!Categories.Contains(category)) return e; ExceptionLogItem log = new ExceptionLogItem(); + log.CallerAssembly = Assembly.GetCallingAssembly().FullName; log.CallerMethodName = caller; log.CallerFile = file; log.CallerLineNumber = lineNumber; @@ -164,6 +166,7 @@ namespace Tango.Logging if (!Categories.Contains(category)) return message; MessageLogItem log = new MessageLogItem(); + log.CallerAssembly = Assembly.GetCallingAssembly().FullName; log.CallerMethodName = caller; log.CallerFile = file; log.CallerLineNumber = lineNumber; diff --git a/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs b/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs index f7d7e004c..f45aa8cea 100644 --- a/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs +++ b/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs @@ -12,18 +12,20 @@ namespace Tango.Logging /// public class MessageLogItem : LogItemBase { + private String _message; /// /// Gets or sets the log message. /// - public String Message { get; set; } - - /// - /// Gets the log message. - /// - /// - public override string GetMessage() + public override string Message { - return Message; + get + { + return _message; + } + set + { + _message = value; + } } /// @@ -31,7 +33,7 @@ namespace Tango.Logging /// public override string ToString() { - return String.Format("[{0}] [{5}] [{1}] [{2}] [Line {3}]: {4}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Message, Category); + return String.Format("[{0}] [{5}] [{1}] [{2}] [{3}]: {4}", TimeStamp.ToString("HH:mm:ss.ff"), GetRelativeCallerFilePath(), CallerMethodName, CallerLineNumber, Message, Category); } } } -- cgit v1.3.1