예제 #1
0
        private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule)
        {
            System.Windows.Controls.RichTextBox rtbx = TargetRichTextBox;

            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 (this.MaxLines > 0)
            {
                this.lineCount++;
                if (this.lineCount > MaxLines)
                {
                    tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd);
                    tr.Text.Remove(0, tr.Text.IndexOf('\n'));
                    this.lineCount--;
                }
            }

            if (this.AutoScroll)
            {
                rtbx.ScrollToEnd();
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 static WpfRichTextBoxRowColoringRule()
 {
     Default = new WpfRichTextBoxRowColoringRule();
 }