/// <summary> /// Actually sends log message to <see cref="TargetRichTextBox"/> /// </summary> /// <param name="logMessage">a message to send</param> /// <param name="rule">matching coloring rule</param> /// <param name="logEvent">original logEvent</param> /// <returns>true if the message was actually sent (i.e. <see cref="TargetRichTextBox"/> is not null and not disposed, and no exception happened during message send)</returns> private bool DoSendMessageToTextbox(string logMessage, RichTextBoxRowColoringRule rule, LogEventInfo logEvent) { RichTextBox textbox = TargetRichTextBox; try { if (textbox != null)//&& !textbox.IsDisposed) { //if (textbox.InvokeRequired) //{ // textbox.BeginInvoke(new DelSendTheMessageToRichTextBox(SendTheMessageToRichTextBox), logMessage, rule, logEvent); //} //else //{ SendTheMessageToRichTextBox(logMessage, rule, logEvent); //} return(true); } } catch (Exception ex) { InternalLogger.Warn(ex.ToString()); if (LogManager.ThrowExceptions) { throw; } } return(false); }
/// <summary> /// Log message to RichTextBox. /// </summary> /// <param name="logEvent">The logging event.</param> protected override void Write(LogEventInfo logEvent) { RichTextBox textbox = TargetRichTextBox; if (textbox == null)// || textbox.IsDisposed) { //no last logged textbox lastLoggedTextBoxControl = null; if (AllowAccessoryFormCreation) { CreateAccessoryForm(); } else if (messageRetention == RichTextBoxTargetMessageRetentionStrategy.None) { InternalLogger.Trace("Textbox for target {0} is {1}, skipping logging", this.Name, textbox == null ? "null" : "disposed"); return; } } string logMessage = Layout.Render(logEvent); RichTextBoxRowColoringRule matchingRule = FindMatchingRule(logEvent); bool messageSent = DoSendMessageToTextbox(logMessage, matchingRule, logEvent); if (messageSent) { //remember last logged text box lastLoggedTextBoxControl = textbox; } switch (messageRetention) { case RichTextBoxTargetMessageRetentionStrategy.None: break; case RichTextBoxTargetMessageRetentionStrategy.All: StoreMessage(logMessage, matchingRule, logEvent); break; case RichTextBoxTargetMessageRetentionStrategy.OnlyMissed: if (!messageSent) { StoreMessage(logMessage, matchingRule, logEvent); } break; default: HandleError("Unexpected retention strategy {0}", messageRetention); break; } }
//internal static void ChangeSelectionToLink(RichTextBox textBox, string text, string hyperlink) //{ // int selectionStart = textBox.SelectionStart; // //using \v tag to hide hyperlink part of the text, and \v0 to end hiding. See http://stackoverflow.com/a/14339531/376066 // //so in the control the link would consist only of "<text>", but in link clicked event we would get "<text>#<hyperlink>" // textBox.SelectedRtf = @"{\rtf1\ansi " + text + @"\v #" + hyperlink + @"\v0}"; // textBox.Select(selectionStart, text.Length + 1 + hyperlink.Length); //now select both visible and invisible part // SetSelectionStyle(textBox, CFM_LINK, CFE_LINK); //and turn into a link //} /// <summary> /// Stores a new message in internal queue, if it exists. Removes overflowing messages. /// </summary> /// <param name="logMessage">a message to store</param> /// <param name="rule">a corresponding coloring rule</param> /// <param name="logEvent">original LogEvent</param> private void StoreMessage(string logMessage, RichTextBoxRowColoringRule rule, LogEventInfo logEvent) { lock (messageQueueLock) { if (messageQueue == null) { return; } if (MaxLines > 0) { while (messageQueue.Count >= MaxLines) { messageQueue.Dequeue(); } } messageQueue.Enqueue(new MessageInfo(logMessage, rule, logEvent)); } }
private void SendTheMessageToRichTextBox(string logMessage, RichTextBoxRowColoringRule rule, LogEventInfo logEvent) { RichTextBox textBox = TargetRichTextBox; //BlockUIContainer block = new BlockUIContainer(); Run run = new Run(logMessage + "\n", textBox.Document.ContentEnd); run.Foreground = new SolidColorBrush(Colors.White); Hyperlink hyperlink = new Hyperlink(run); hyperlink.NavigateUri = new Uri("http://www.baidu.com"); //hyperlink.NavigateUri = new Uri(logMessage); //Paragraph myParagraph = new Paragraph(run); Paragraph myParagraph = new Paragraph(hyperlink); myParagraph.Margin = new Thickness(0); myParagraph.Padding = new Thickness(0); //myParagraph.Inlines.Add(run); textBox.Document.Blocks.Add(myParagraph); //int startIndex = textBox.Text.Length; //textBox.SelectionStart = startIndex; //textBox.SelectionBackColor = GetColorFromString(rule.BackgroundColor, textBox.BackColor); //textBox.SelectionColor = GetColorFromString(rule.FontColor, textBox.ForeColor); //textBox.SelectionFont = new Font(textBox.SelectionFont, textBox.SelectionFont.Style ^ rule.Style); //textBox.AppendText(logMessage + "\n"); //textBox.SelectionLength = textBox.Text.Length - textBox.SelectionStart; //// find word to color //foreach (RichTextBoxWordColoringRule wordRule in WordColoringRules) //{ // MatchCollection matches = wordRule.CompiledRegex.Matches(textBox.Text, startIndex); // foreach (Match match in matches) // { // textBox.SelectionStart = match.Index; // textBox.SelectionLength = match.Length; // textBox.SelectionBackColor = GetColorFromString(wordRule.BackgroundColor, textBox.BackColor); // textBox.SelectionColor = GetColorFromString(wordRule.FontColor, textBox.ForeColor); // textBox.SelectionFont = new Font(textBox.SelectionFont, textBox.SelectionFont.Style ^ wordRule.Style); // } //} //if (SupportLinks) //{ // object linkInfoObj; // lock (logEvent.Properties) // { // logEvent.Properties.TryGetValue(RichTextBoxLinkLayoutRenderer.LinkInfo.PropertyName, out linkInfoObj); // } // if (linkInfoObj != null) // { // RichTextBoxLinkLayoutRenderer.LinkInfo linkInfo = (RichTextBoxLinkLayoutRenderer.LinkInfo)linkInfoObj; // bool linksAdded = false; // textBox.SelectionStart = startIndex; // textBox.SelectionLength = textBox.Text.Length - textBox.SelectionStart; // string addedText = textBox.SelectedText; // MatchCollection matches = linkAddRegex.Matches(addedText); //only access regex after checking SupportLinks, as it assures the initialization // for (int i = matches.Count - 1; i >= 0; --i) //backwards order, so the string positions are not affected // { // Match match = matches[i]; // string linkText = linkInfo.GetValue(match.Value); // if (linkText != null) // { // textBox.SelectionStart = startIndex + match.Index; // textBox.SelectionLength = match.Length; // FormHelper.ChangeSelectionToLink(textBox, linkText, LinkPrefix + logEvent.SequenceID); // linksAdded = true; // } // } // if (linksAdded) // { // linkedEvents[logEvent.SequenceID] = logEvent; // } // } //} ////remove some lines if there above the max //if (MaxLines > 0) //{ // //find the last line by reading the textbox // var lastLineWithContent = textBox.Lines.LastOrDefault(f => !string.IsNullOrEmpty(f)); // if (lastLineWithContent != null) // { // char lastChar = lastLineWithContent.Last(); // var visibleLineCount = textBox.GetLineFromCharIndex(textBox.Text.LastIndexOf(lastChar)); // var tooManyLines = (visibleLineCount - MaxLines) + 1; // if (tooManyLines > 0) // { // textBox.SelectionStart = 0; // textBox.SelectionLength = textBox.GetFirstCharIndexFromLine(tooManyLines); // if (SupportLinks) // { // string selectedRtf = textBox.SelectedRtf; // //only access regex after checking SupportLinks, as it assures the initialization // foreach (Match match in linkRemoveRtfRegex.Matches(selectedRtf)) // { // int id; // if (int.TryParse(match.Groups[1].Value, out id)) // { // lock (linkedEventsLock) // { // linkedEvents.Remove(id); // } // } // } // } // textBox.SelectedRtf = "{\\rtf1\\ansi}"; // } // } //} //if (AutoScroll) //{ // textBox.Select(textBox.TextLength, 0); // textBox.ScrollToCaret(); //} 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); //tr.ApplyPropertyValue(Inline.TextDecorationsProperty,TextDecorations.Baseline); //tr.ApplyPropertyValue(Hyperlink.NavigateUriProperty, new Uri("http://www.baidu.com")); //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(); //} }
internal MessageInfo(string message, RichTextBoxRowColoringRule rule, LogEventInfo logEvent) { this.Message = message; this.Rule = rule; this.LogEvent = logEvent; }