public JournalLine ReadCurrent() { if (!File.Exists) { return(null); } using (var file = new FileStream(File.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var stream = new StreamReader(file)) { var content = stream.ReadToEnd(); if (string.IsNullOrWhiteSpace(content)) { return(null); } try { var(_, timestamp) = _parser.ParseCommonProperties(content); if (timestamp > _lastSeen) { _lastSeen = timestamp; return(new JournalLine(Context, content)); } } catch (JournalParseException e) { Log.Warn($"Unable to read content of {File.Name}", e); return(null); } } } return(null); }
private JournalLogFileInfo ReadFileInfo() { FileHeader fileHeader = null; var info = new JournalLogFileInfo(File); DateTimeOffset?lastEntry = null; try { // Get a new reader for this action only using (var streamReader = GetStreamReader()) { // FileHeader *should* be the first line in the file, but at least try the first 5 var tolerance = 5; while (!streamReader.EndOfStream && tolerance > 0 && fileHeader == null) { var line = streamReader.ReadLine(); if (string.IsNullOrWhiteSpace(line)) { continue; } try { var(eventType, timestamp) = _parser.ParseCommonProperties(line); if (timestamp > (lastEntry ?? DateTimeOffset.MinValue)) { lastEntry = timestamp; } if (eventType?.Equals(nameof(FileHeader), StringComparison.InvariantCultureIgnoreCase) == true) { fileHeader = _parser.Parse <FileHeader>(line); } } catch (JournalParseException) { // Cannot parse line, don't consider as potential fileheader } tolerance--; } } if (fileHeader != null) { info = new JournalLogFileInfo(File, fileHeader.GameVersion, fileHeader.Timestamp, lastEntry ?? fileHeader.Timestamp); } } catch (FileNotFoundException e) { Log.Error($"Could not read file '{e.FileName}'", e); } catch (IOException e) { Log.Error($"Could not read file '{File.FullName}'", e); } return(info); }