public void LoadFile(string path) { try { if (!File.Exists(path)) { return; } var alreadyOpened = Items.FirstOrDefault(it => it.ToolTip == path); if (alreadyOpened != null) { ActivateItem(alreadyOpened); return; } var fileReceiver = new FileReceiver { FileToWatch = path, LogFormat = _logReaderFactory.GetLogFormatByFileExtension(Path.GetExtension(path)) }; AddReceiver(fileReceiver); } catch (Exception e) { DialogService.Current.ShowErrorMessageBox(e); } }
public override async Task <IEnumerable <AnalogyLogMessage> > Process(string fileName, CancellationToken token, ILogMessageCreatedHandler messagesHandler) { List <AnalogyLogMessage> messages = new List <AnalogyLogMessage>(); void FileReceiverNewMessages(object sender, IReadOnlyCollection <Logazmic.Core.Log.LogMessage> e) { var newMessages = new List <AnalogyLogMessage>(); foreach (LogMessage log in e) { AnalogyLogMessage m = new AnalogyLogMessage(log.Message, GetLogLevel(log.LogLevel), AnalogyLogClass.General, log.CallSiteClass, "", "", "", 0, 0, null, "", log.CallSiteMethod, log.SourceFileName, (int)log.SourceFileLineNr); FillProperties(m, log); newMessages.Add(m); } messages.AddRange(newMessages); messagesHandler.AppendMessages(newMessages, fileName); } void FileReceiverNewMessage(object sender, Logazmic.Core.Log.LogMessage log) { AnalogyLogMessage m = new AnalogyLogMessage(log.Message, GetLogLevel(log.LogLevel), AnalogyLogClass.General, log.CallSiteClass, "", "", "", 0, 0, null, "", log.CallSiteMethod, log.SourceFileName, (int)log.SourceFileLineNr); messages.Add(m); messagesHandler.AppendMessage(m, fileName); } var fileReceiver = new FileReceiver { LogReaderFactory = _logReaderFactory, FileToWatch = fileName, LogFormat = _logReaderFactory.GetLogFormatByFileExtension(Path.GetExtension(fileName)) }; try { var tcs = new TaskCompletionSource <IEnumerable <AnalogyLogMessage> >(); fileReceiver.NewMessage += FileReceiverNewMessage; fileReceiver.NewMessages += FileReceiverNewMessages; fileReceiver.OnDoneReadingFile += (s, e) => { tcs.SetResult(messages); }; fileReceiver.Initialize(); var result = await tcs.Task.ConfigureAwait(false); return(result); } catch (Exception e) { Analogy.LogViewer.Template.Managers.LogManager.Instance.LogException($"Error reading file: {e.Message}", e, nameof(Process)); return(messages); } finally { fileReceiver.NewMessage -= FileReceiverNewMessage; fileReceiver.NewMessages -= FileReceiverNewMessages; } }