private void AddExistingLogEntries() { lock (this.logger.LogLock) { // Add all existing log entries ColoredLogGroup currentGroup = null; foreach (LogEntry entry in this.logger.LogEntries) { LogColor entryColor = GetEntryColor(entry); // If we need to start a new group if (currentGroup == null || entryColor != currentGroup.Color) { // Write out the last group (if it exists) if (currentGroup != null) { this.AddLogGroup(currentGroup); } // Start the new group currentGroup = new ColoredLogGroup(entryColor); } // Add the entry currentGroup.Entries.Add(entry); } // Add any items in the last group if (currentGroup != null) { this.AddLogGroup(currentGroup); } } }
private void AddExistingLogEntries() { lock (this.logger.LogLock) { this.logger.SuspendWriter(); try { // Add all existing log entries ColoredLogGroup currentGroup = null; using (var reader = new StreamReader(new FileStream(this.logger.LogPath, FileMode.Open, FileAccess.Read))) { string line; while ((line = reader.ReadLine()) != null) { LogColor color = LogEntryClassificationUtilities.GetLineColor(line); // If we need to start a new group if (currentGroup == null || color != currentGroup.Color) { // Write out the last group (if it exists) if (currentGroup != null) { this.AddLogGroup(currentGroup); } // Start the new group currentGroup = new ColoredLogGroup(color); } // Add the entry currentGroup.Entries.Add(line); } } // Add any items in the last group if (currentGroup != null) { this.AddLogGroup(currentGroup); } } catch (Exception exception) { var errorLogGroup = new ColoredLogGroup(LogColor.Error); errorLogGroup.Entries.Add($"Could not load log file {this.logger.LogPath}" + Environment.NewLine + exception); this.AddLogGroup(errorLogGroup); } finally { this.logger.ResumeWriter(); } } }
private void AddLogGroup(ColoredLogGroup group) { Brush brush = this.logColorBrushMapping[group.Color]; StringBuilder runText = new StringBuilder(); foreach (LogEntry entry in group.Entries) { runText.AppendLine(entry.Text); } var run = new Run(runText.ToString()); run.Foreground = brush; this.logParagraph.Inlines.Add(run); }
public LogWindow() { InitializeComponent(); lock (this.logger.LogLock) { // Add all existing log entries ColoredLogGroup currentGroup = null; foreach (LogEntry entry in this.logger.LogEntries) { Color entryColor = GetEntryColor(entry); // If we need to start a new group if (currentGroup == null || entryColor != currentGroup.Color) { // Write out the last group (if it exists) if (currentGroup != null) { this.AddLogGroup(currentGroup); } // Start the new group currentGroup = new ColoredLogGroup(entryColor); } // Add the entry currentGroup.Entries.Add(entry); } // Add any items in the last group if (currentGroup != null) { this.AddLogGroup(currentGroup); } } this.Loaded += (sender, e) => { this.logTextBox.ScrollToEnd(); }; // Subscribe to events this.logger.EntryLogged += this.OnEntryLogged; this.logger.Cleared += this.OnCleared; }
// Adds pending entries to the UI private void ProcessPendingEntries() { List <ColoredLogGroup> entryGroups; lock (this.pendingEntriesLock) { if (this.pendingEntries.Count == 0) { // If there are no items left, scroll to the end if we're already there, then bail if (this.logTextBox.VerticalOffset + this.logTextBox.ViewportHeight >= this.logTextBox.ExtentHeight) { this.Dispatcher.BeginInvoke(new Action(() => { this.logTextBox.ScrollToEnd(); })); } this.workerRunning = false; return; } // copy some pending items to groups entryGroups = new List <ColoredLogGroup>(); ColoredLogGroup currentGroup = null; int currentLines = 0; while (this.pendingEntries.Count > 0 && entryGroups.Count < MaxRunsPerDispatch && currentLines < MaxLinesPerDispatch) { LogEntry entry = this.pendingEntries.Dequeue(); Color entryColor = GetEntryColor(entry); if (currentGroup == null || entryColor != currentGroup.Color) { currentGroup = new ColoredLogGroup(entryColor); entryGroups.Add(currentGroup); } currentGroup.Entries.Add(entry); currentLines++; } } this.AddLogGroups(entryGroups); this.Dispatcher.BeginInvoke(new Action(this.ProcessPendingEntries)); }
// Adds pending entries to the UI private void ProcessPendingEntries() { List <ColoredLogGroup> entryGroups; lock (this.pendingEntriesLock) { if (this.pendingEntries.Count == 0) { // If we are already at the bottom, scroll to the end, which will fire after the entries have been added to the UI. if (this.logTextBox.VerticalOffset + this.logTextBox.ViewportHeight >= this.logTextBox.ExtentHeight) { this.logTextBox.ScrollToEnd(); } this.workerRunning = false; return; } // copy some pending items to groups entryGroups = new List <ColoredLogGroup>(); ColoredLogGroup currentGroup = null; int currentLines = 0; while (this.pendingEntries.Count > 0 && entryGroups.Count < MaxRunsPerDispatch && currentLines < MaxLinesPerDispatch) { LogEntry entry = this.pendingEntries.Dequeue(); LogColor entryColor = GetEntryColor(entry); if (currentGroup == null || entryColor != currentGroup.Color) { currentGroup = new ColoredLogGroup(entryColor); entryGroups.Add(currentGroup); } currentGroup.Entries.Add(entry); currentLines++; } } this.AddLogGroups(entryGroups); this.ProcessPendingEntries(); }