private void SendTheMessageToRichTextBox(string logMessage, RichTextBoxRowColoringRule rule, LogEventInfo logEvent) { RichTextBox textBox = TargetRichTextBox; 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 LINKS_SUPPORTED 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; } } } #endif //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 LINKS_SUPPORTED 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); } } } } #endif textBox.SelectedRtf = "{\\rtf1\\ansi}"; } } } if (AutoScroll) { textBox.Select(textBox.TextLength, 0); textBox.ScrollToCaret(); } }
/// <summary> /// Initializes the target. Can be used by inheriting classes /// to initialize logging. /// </summary> protected override void InitializeTarget() { base.InitializeTarget(); if (TargetRichTextBox != null) { //already initialized by ReInitializeAllTextboxes call return; } CreatedForm = false; Form openFormByName; RichTextBox targetControl; if (AllowAccessoryFormCreation) { //old behaviour which causes creation of accessory form in case specified control cannot be found on specified form if (FormName == null) { InternalLogger.Info("FormName not set, creating acceccory form"); CreateAccessoryForm(); return; } openFormByName = Application.OpenForms[FormName]; if (openFormByName == null) { InternalLogger.Info("Form {0} not found, creating accessory form", FormName); CreateAccessoryForm(); return; } if (string.IsNullOrEmpty(ControlName)) { HandleError("Rich text box control name must be specified for {0}.", GetType().Name); CreateAccessoryForm(); return; } targetControl = FormHelper.FindControl <RichTextBox>(ControlName, openFormByName); if (targetControl == null) { HandleError("Rich text box control '{0}' cannot be found on form '{1}'.", ControlName, FormName); CreateAccessoryForm(); return; } //finally attached to proper control } else { //new behaviour which postpones attaching to textbox if it's not yet available at the time, if (FormName == null) { HandleError("FormName should be specified for {0}.{1}", GetType().Name, this.Name); return; } if (string.IsNullOrEmpty(ControlName)) { HandleError("Rich text box control name must be specified for {0}.{1}", GetType().Name, this.Name); return; } openFormByName = Application.OpenForms[FormName]; if (openFormByName == null) { InternalLogger.Info("Form {0} not found, waiting for ReInitializeAllTextboxes.", FormName); return; } targetControl = FormHelper.FindControl <RichTextBox>(ControlName, openFormByName); if (targetControl == null) { InternalLogger.Info("Rich text box control '{0}' cannot be found on form '{1}'. Waiting for ReInitializeAllTextboxes.", ControlName, FormName); return; } //actually attached to a target, all ok } AttachToControl(openFormByName, targetControl); }