public void DisplayLog() { lock (_lock) { if (!File.Exists(_filePath)) { // If the log file is not accessible, display the contents of the in memory buffer and anything saved in the log buffer _mainUi.LogErrorToUi($"Could not read the log file: {_filePath}. File does not exist", false, false); if (_memLogMessages != null && _memLogMessages.Count > 0) { _mainUi.LogErrorToUi($"Displaying last {_memLogMessages.Count} saved log messages", false, false); string[] arr = _memLogMessages.ToArray(); foreach (var s in arr) { _mainUi.LogToUi(s, false, false); } } if (_logBuffer != null && _logBuffer.Length > 0) { _mainUi.LogErrorToUi($"Displaying messages since log file became unavailable", false, false); _mainUi.LogToUi(_logBuffer.ToString(), false, false); } return; } // Read the log contents and display in the log tab. var lines = new List <string>(); var truncated = false; using ( var reader = new StreamReader(new FileStream(GetFile(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ) { var maxDisplaySize = MAX_LOG_SIZE / 20; // If the log is too big don't display all of it. if (reader.BaseStream.Length > maxDisplaySize) { reader.BaseStream.Seek(-maxDisplaySize, SeekOrigin.End); truncated = true; } string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } if (lines.Count > MAX_LOG_LINES) { lines = lines.GetRange(lines.Count - MAX_LOG_LINES - 1, MAX_LOG_LINES); truncated = true; } } if (truncated) { _mainUi.LogErrorToUi(string.Format(LogTruncatedMessage, GetFile()), false); } var toLog = new List <string>(); var lastLineErr = false; foreach (var line in lines) { var error = line.ToLower().Contains("error"); if (error) { if (!lastLineErr && toLog.Count > 0) { _mainUi.LogLinesToUi(toLog); toLog.Clear(); } lastLineErr = true; toLog.Add(line); } else { if (lastLineErr && toLog.Count > 0) { _mainUi.LogErrorLinesToUi(toLog); toLog.Clear(); } lastLineErr = false; toLog.Add(line); } } if (toLog.Count > 0) { if (lastLineErr) { _mainUi.LogErrorLinesToUi(toLog); } else { _mainUi.LogLinesToUi(toLog); } } } }
public void DisplayLog() { lock (_lock) { // Read the log contents and display in the log tab. var lines = new List <string>(); var truncated = false; using ( var reader = new StreamReader(new FileStream(GetFile(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ) { var maxDisplaySize = MaxLogSize / 20; // If the log is too big don't display all of it. if (reader.BaseStream.Length > maxDisplaySize) { reader.BaseStream.Seek(-maxDisplaySize, SeekOrigin.End); truncated = true; } string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } if (lines.Count > MaxLogLines) { lines = lines.GetRange(lines.Count - MaxLogLines - 1, MaxLogLines); truncated = true; } } if (truncated) { _mainUi.LogErrorToUi(string.Format(LogTruncatedMessage, GetFile()), false); } var toLog = new List <string>(); var lastLineErr = false; foreach (var line in lines) { var error = line.ToLower().Contains("error"); if (error) { if (!lastLineErr && toLog.Count > 0) { _mainUi.LogLinesToUi(toLog); toLog.Clear(); } lastLineErr = true; toLog.Add(line); } else { if (lastLineErr && toLog.Count > 0) { _mainUi.LogErrorLinesToUi(toLog); toLog.Clear(); } lastLineErr = false; toLog.Add(line); } } if (toLog.Count > 0) { if (lastLineErr) { _mainUi.LogErrorLinesToUi(toLog); } else { _mainUi.LogLinesToUi(toLog); } } } }