How can I use the NLog RichTextBox Target in a WPF application?

How to use RichTextBox Target in a WPF application? I don’t want to have a separate log window, I want all log messages to be displayed in richTextBox located in the WPF dialog box.

I tried to use WindowsFormsHost with the RichTextBox core inside, but this did not work for me: NLog in any case opened a separate Windows Form.

+2
source share
2 answers

If you define RichTextBoxTarget in the configuration file, a new form is automatically created. This is because NLog is initialized before your (named) form and control have been created. Even if you don’t have rules indicating a goal. There may be a better solution, but I solved it by creating the target programmatically:

using NLog; //[...] RichTextBoxTarget target = new RichTextBoxTarget(); target.Name = "RichTextBox"; target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}"; target.ControlName = "textbox1"; target.FormName = "Form1"; target.AutoScroll = true; target.MaxLines = 10000; target.UseDefaultRowColoringRules = false; target.RowColoringRules.Add( new RichTextBoxRowColoringRule( "level == LogLevel.Trace", // condition "DarkGray", // font color "Control", // background color FontStyle.Regular ) ); target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Control")); target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "ControlText", "Control")); target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "DarkRed", "Control")); target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Error", "White", "DarkRed", FontStyle.Bold)); target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "Yellow", "DarkRed", FontStyle.Bold)); AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper(); asyncWrapper.Name = "AsyncRichTextBox"; asyncWrapper.WrappedTarget = target; SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Trace); 
-2
source

A workaround in the average case is to use the 3 available classes here , and then do the following procedure:

  • Import 3 files into the project

  • If not already, use Project > Add Reference to add references to WPF assemblies: WindowsBase, PresentationCore, PresentationFramework .

  • In WpfRichTextBoxTarget.cs replace lines 188-203 with:

      //this.TargetRichTextBox.Invoke(new DelSendTheMessageToRichTextBox(this.SendTheMessageToRichTextBox), new object[] { logMessage, matchingRule }); if (System.Windows.Application.Current.Dispatcher.CheckAccess() == false) { System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { SendTheMessageToRichTextBox(logMessage, matchingRule); })); } else { SendTheMessageToRichTextBox(logMessage, matchingRule); } } private static Color GetColorFromString(string color, Brush defaultColor) { if (defaultColor == null) return Color.FromRgb(255, 255, 255); // This will set default background colour to white. if (color == "Empty") { return (Color)colorConverter.ConvertFrom(defaultColor); } return (Color)colorConverter.ConvertFromString(color); } 
  • In your code, set up a new target as shown below:

I hope this helps, but it definitely doesn't look like a comprehensive implementation ...

 public void loading() { var target = new WpfRichTextBoxTarget(); target.Name = "console"; target.Layout = "${longdate:useUTC=true}|${level:uppercase=true}|${logger}::${message}"; target.ControlName = "rtbConsole"; // Name of the richtextbox control already on your window target.FormName = "MonitorWindow"; // Name of your window where there is the richtextbox, but it seems it will not really be taken into account, the application mainwindow will be used instead. target.AutoScroll = true; target.MaxLines = 100000; target.UseDefaultRowColoringRules = true; AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper(); asyncWrapper.Name = "console"; asyncWrapper.WrappedTarget = target; SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Trace); } 
+2
source

All Articles