private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule) { System.Windows.Controls.RichTextBox rtbx = TargetRichTextBox; var scrolledToEnd = AutoScroll && (TargetRichTextBox.VerticalOffset + TargetRichTextBox.ViewportHeight) >= (TargetRichTextBox.ExtentHeight - .1); var tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd); tr.Text = logMessage + "\n"; tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(GetColorFromString(rule.FontColor, (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty))) ); tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(GetColorFromString(rule.BackgroundColor, (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty))) ); tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style); tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight); if (MaxLines > 0) { while (rtbx.Document.Blocks.Count - 1 > MaxLines) { rtbx.Document.Blocks.Remove(rtbx.Document.Blocks.FirstBlock); } } if (AutoScroll && scrolledToEnd) { rtbx.ScrollToEnd(); } }
protected override void Write(LogEventInfo logEvent) { WpfRichTextBoxRowColoringRule matchingRule = RowColoringRules.FirstOrDefault(rr => rr.CheckCondition(logEvent)); if (UseDefaultRowColoringRules && matchingRule == null) { foreach (var rr in DefaultRowColoringRules.Where(rr => rr.CheckCondition(logEvent))) { matchingRule = rr; break; } } if (matchingRule == null) { matchingRule = WpfRichTextBoxRowColoringRule.Default; } var logMessage = Layout.Render(logEvent); if (System.Windows.Application.Current == null) { return; } try { if (System.Windows.Application.Current.Dispatcher.CheckAccess() == false) { System.Windows.Application.Current.Dispatcher.Invoke(() => SendTheMessageToRichTextBox(logMessage, matchingRule)); } else { SendTheMessageToRichTextBox(logMessage, matchingRule); } } catch (Exception ex) { Debug.WriteLine(ex); } }
static WpfRichTextBoxRowColoringRule() { Default = new WpfRichTextBoxRowColoringRule(); }