diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2018-12-16 08:22:45 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2018-12-16 08:22:45 +0200 |
| commit | ecf55f4193c0a7ab273c7e8243e446a2f2c32d51 (patch) | |
| tree | aa965e23ba3d0dcd4fecf739b4ed95040553b628 /Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs | |
| parent | b279560f0b4dfdd16dea6b70975dfc1961b8d61e (diff) | |
| download | Tango-ecf55f4193c0a7ab273c7e8243e446a2f2c32d51.tar.gz Tango-ecf55f4193c0a7ab273c7e8243e446a2f2c32d51.zip | |
Working on PPC tech mode & logging module.
Working on PPC date picker & calendar.
Implemented no-permissions view.
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs b/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs new file mode 100644 index 000000000..2870ce95d --- /dev/null +++ b/Software/Visual_Studio/Tango.Logging/ApplicationLogFileParser.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Tango.Logging; + +namespace Tango.Logging +{ + public class ApplicationLogFileParser : ILogFileParser<LogItemBase> + { + public List<LogFile> GetLogFiles() + { + 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)) + { + 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 }); + } + catch (Exception ex) + { + LogManager.Default.Log(ex, $"Could not load application log file {Path.GetFileName(file)}"); + } + } + + return logFiles; + } + + public List<LogItemBase> Parse(LogFile logFile) + { + List<LogItemBase> logItems = new List<LogItemBase>(); + + String text = File.ReadAllText(logFile.File); + var logs = Regex.Split(text, @"(\[\d{2}:\d{2}:\d{2}.\d{2}\])"); + + for (int i = 1; i < logs.Length; i += 2) + { + try + { + DateTime date = DateTime.ParseExact(logs[i].Replace("[", "").Replace("]", ""), "HH:mm:ss.ff", CultureInfo.InvariantCulture); + String rest = logs[i + 1]; + + 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.Category = (LogCategory)Enum.Parse(typeof(LogCategory), entries[1]); + item.CallerFile = entries[3]; + item.CallerMethodName = entries[5]; + item.CallerLineNumber = int.Parse(entries[7]); + item.Message = new String(entries[8].Skip(2).ToArray()); + + logItems.Add(item); + } + catch (Exception ex) + { + LogManager.Default.Log(ex, "Could not parse log line: " + logs[i]); + } + } + + return logItems; + } + } +} |
