//private string GetLogListener(string fileName) //{ // var characterName = string.Empty; // using (var sr = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), _fileEncoding)) // { // try // { // while (sr.Peek() >= 0) // { // var line = CleanLineData(sr.ReadLine()); // var lineMatch = _localListener.Match(line ?? string.Empty); // if (lineMatch.Success) // { // characterName = lineMatch.Groups["name"].ToString(); // break; // } // } // } // catch (Exception) // { // return characterName; // } // } // return characterName; //} private LocalInfo InitLocal(string fileName) { LocalInfo tempInfo = new LocalInfo(); using (var sr = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), _fileEncoding)) { try { while (sr.Peek() >= 0) { var line = CleanLineData(sr.ReadLine()); if (tempInfo.InitialSystem == -1) { var lineMatch = _localInitialSystem.Match(line ?? string.Empty); if (lineMatch.Success) { tempInfo.InitialSystem = int.Parse(lineMatch.Groups["initialsystem"].ToString()); continue; } } if (tempInfo.CharName == string.Empty) { var lineMatch = _localListener.Match(line ?? string.Empty); if (lineMatch.Success) { tempInfo.CharName = lineMatch.Groups["name"].ToString(); if (tempInfo.CharName.Trim().Length <= 4) { tempInfo.CharName = string.Empty; } continue; } } var systemChangeMatch = _localSystemChange.Match(line ?? string.Empty); if (systemChangeMatch.Success) { tempInfo.CurrentSystem = systemChangeMatch.Groups["systemname"].ToString(); } } } catch (Exception) { return(new LocalInfo()); } } return(tempInfo); }
private void OnTick(object sender, EventArgs eventArgs) { Enabled = false; try { // Process new files List <FileInfo> logFiles = GetChangedLogFiles(); foreach (var fileInfo in logFiles) { lock (this) { if (_interestingFiles.ContainsKey(fileInfo.FullName)) { continue; } long tempLength = GetFileLength(fileInfo.FullName); LocalInfo tempLocalInfo = InitLocal(fileInfo.FullName); _interestingFiles.Add(fileInfo.FullName, new InterestingFile(fileInfo.FullName, tempLength, DateTime.Now, tempLocalInfo.CharName)); OnSystemChange( tempLocalInfo.CurrentSystem == string.Empty ? tempLocalInfo.InitialSystem.ToString() : tempLocalInfo.CurrentSystem, tempLocalInfo.CharName); } } // Remove files with no update in last 120 mins from interesting files list List <string> uninterestingFiles = (from interestingFile in _interestingFiles where interestingFile.Value.LastUpdate < DateTime.Now.AddMinutes(-120) select interestingFile.Key).ToList(); foreach (var uninterestingFile in uninterestingFiles) { lock (this) { _interestingFiles.Remove(uninterestingFile); } } // Get new content from all interesting files lock (this) { foreach (var interestingFile in _interestingFiles) { var tempFileInfo = new FileInfo(interestingFile.Key); var tempFileLength = GetFileLength(tempFileInfo.FullName); if (tempFileLength != interestingFile.Value.LastPosition) { NewData newData; lock (this) { newData = ReadLogFile(interestingFile.Key, interestingFile.Value.LastPosition); _interestingFiles[interestingFile.Key].LastPosition = newData.FileLength; _interestingFiles[interestingFile.Key].LastUpdate = DateTime.Now; } var lines = newData.Data.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); lines = lines.Where(line => line.Length > 0).ToArray(); //var lines = newData.Data.Split(new[] {"\n"}, StringSplitOptions.RemoveEmptyEntries); //lines = lines.Select(line => line.Replace(Convert.ToChar(65279).ToString(), "").Replace("\r", "").Trim()).Where(line => line.Length > 0).ToArray(); //lines = lines.Select(line => line.Trim()).Where(line => line.Length > 0).ToArray(); if (lines.Length == 0) { continue; } foreach (var line in lines) { var lineMatch = _localSystemChange.Match(line); if (lineMatch.Success) { OnSystemChange(lineMatch.Groups["systemname"].ToString(), interestingFile.Value.CharName); } } } } } } finally { Enabled = true; } }