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);
source share