public bool TryParse(string line, RegexPattern regex, out AnalogyLogMessage?message) { try { Match match = Regex.Match(line, regex.Pattern, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace); if (match.Success) { var properties = new List <(AnalogyLogMessagePropertyName, string)>(); foreach (var regexMember in regexMapper) { string value = match.Groups[regexMember.Key].Success ? match.Groups[regexMember.Key].Value : string.Empty; properties.Add((regexMember.Value, value)); } message = AnalogyLogMessage.Parse(properties); return(true); } message = null; return(false); } catch (Exception e) { string error = $"Error parsing line: {e.Message}"; Logger?.LogException(error, e, nameof(RegexParser)); message = new AnalogyLogMessage(error, AnalogyLogLevel.Error, AnalogyLogClass.General, nameof(RegexParser)); return(false); } }
public AnalogyLogMessage Parse(string line) { var items = line.Split(splitters, StringSplitOptions.RemoveEmptyEntries).ToList(); List <(AnalogyLogMessagePropertyName, string)> map = new List <(AnalogyLogMessagePropertyName, string)>(); for (int i = 0; i < items.Count; i++) { var item = items[i]; if (_logFileSettings.Maps.ContainsKey(i)) { map.Add((_logFileSettings.Maps[i], items[i])); } } return(AnalogyLogMessage.Parse(map)); }
public async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token, ILogMessageCreatedHandler messagesHandler) { if (string.IsNullOrEmpty(fileName)) { AnalogyLogMessage empty = new AnalogyLogMessage($"File is null or empty. Aborting.", AnalogyLogLevel.Critical, AnalogyLogClass.General, "Analogy", "None") { Source = "Analogy", Module = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; messagesHandler.AppendMessage(empty, GetFileNameAsDataSource(fileName)); return(new List <AnalogyLogMessage> { empty }); } if (!_logFileSettings.IsConfigured) { AnalogyLogMessage empty = new AnalogyLogMessage( $"File Parser is not configured. Please set it first in the settings Window", AnalogyLogLevel.Critical, AnalogyLogClass.General, "Analogy", "None") { Source = "Analogy", Module = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; messagesHandler.AppendMessage(empty, GetFileNameAsDataSource(fileName)); return(new List <AnalogyLogMessage> { empty }); } if (!_logFileSettings.CanOpenFile(fileName)) { AnalogyLogMessage empty = new AnalogyLogMessage( $"File {fileName} Is not supported or not configured correctly in the windows settings", AnalogyLogLevel.Critical, AnalogyLogClass.General, "Analogy", "None") { Source = "Analogy", Module = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; messagesHandler.AppendMessage(empty, GetFileNameAsDataSource(fileName)); return(new List <AnalogyLogMessage> { empty }); } List <AnalogyLogMessage> messages = new List <AnalogyLogMessage>(); try { string xmlData = File.ReadAllText(fileName); List <List <(string, string)> > ParseData(string data) => ParserInternal(data); try { var rows = ParseData(xmlData); foreach (var items in rows) { List <(string, string)> tuples = new List <(string, string)>(items.Count); foreach (var(key, value) in items) { var keyProperty = _logFileSettings.GetAnalogyPropertyName(key); tuples.Add(keyProperty.HasValue ? (keyProperty.Value.ToString(), value) : (key, value)); } var m = AnalogyLogMessage.Parse(tuples); messages.Add(m); } } catch (Exception e) { string data = TryFix(xmlData); ParseData(data); } messagesHandler.AppendMessages(messages, fileName); return(messages); } catch (Exception e) { AnalogyLogMessage empty = new AnalogyLogMessage($"Error occured processing file {fileName}. Reason: {e.Message}", AnalogyLogLevel.Critical, AnalogyLogClass.General, "Analogy", "None") { Source = "Analogy", Module = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; messagesHandler.AppendMessage(empty, GetFileNameAsDataSource(fileName)); return(new List <AnalogyLogMessage> { empty }); } }
private List <AnalogyLogMessage> ProcessJsonData(string json, string fileName, ILogMessageCreatedHandler messagesHandler, bool reportProgress) { List <AnalogyLogMessage> messages = new List <AnalogyLogMessage>(); try { var items = JsonConvert.DeserializeObject <dynamic>(json); if (items is JArray jArray) { for (var i = 0; i < jArray.Count; i++) { var item = jArray[i]; var itemProperties = item.Children <JProperty>().ToList(); List <(string, string)> tuples = new List <(string, string)>(itemProperties.Count); List <(string, string)> nonAnalogyTuples = new List <(string, string)>(itemProperties.Count); foreach (var jprop in itemProperties) { if (UserSettingsManager.UserSettings.Settings.TryGetAnalogyValue(jprop.Name, out var prop)) { tuples.Add((prop.ToString(), jprop.Value.ToString())); } else { nonAnalogyTuples.Add((jprop.Name, jprop.Value.ToString())); } } var m = AnalogyLogMessage.Parse(tuples); m.RawText = item.ToString(); m.RawTextType = AnalogyRowTextType.JSON; m.AdditionalInformation = new Dictionary <string, string>(); foreach (var t in nonAnalogyTuples) { m.AdditionalInformation.Add(t.Item1, t.Item2); } messages.Add(m); if (reportProgress) { messagesHandler.ReportFileReadProgress(new AnalogyFileReadProgress(AnalogyFileReadProgressType.Percentage, 1, i, jArray.Count)); } } } else if (items is JObject jObject) { var itemProperties = jObject.Children <JProperty>().ToList(); List <(string, string)> tuples = new List <(string, string)>(itemProperties.Count); List <(string, string)> nonAnalogyTuples = new List <(string, string)>(itemProperties.Count); foreach (var jprop in itemProperties) { if (UserSettingsManager.UserSettings.Settings.TryGetAnalogyValue(jprop.Name, out var prop)) { tuples.Add((prop.ToString(), jprop.Value.ToString())); } else { nonAnalogyTuples.Add((jprop.Name, jprop.Value.ToString())); } } var m = AnalogyLogMessage.Parse(tuples); m.RawText = json; m.RawTextType = AnalogyRowTextType.JSON; m.AdditionalInformation = new Dictionary <string, string>(); foreach (var t in nonAnalogyTuples) { m.AdditionalInformation.Add(t.Item1, t.Item2); } messages.Add(m); if (reportProgress) { messagesHandler.ReportFileReadProgress(new AnalogyFileReadProgress(AnalogyFileReadProgressType.Percentage, 1, 1, 1)); } } messagesHandler.AppendMessages(messages, fileName); return(messages); } catch (Exception e) { AnalogyLogMessage empty = new AnalogyLogMessage($"Error occurred processing file {fileName}. Reason: {e.Message}", AnalogyLogLevel.Critical, AnalogyLogClass.General, "Analogy", "None") { Source = "Analogy", Module = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; messagesHandler.AppendMessage(empty, Utils.GetFileNameAsDataSource(fileName)); return(new List <AnalogyLogMessage> { empty }); } }