我想在应用程序执行的同时显示NLog
跟踪RichTextBox
logger.Trace("This is a Trace message"); logger.Debug("This is a Debug message"); logger.Info("This is an Info message"); logger.Warn("This is a Warn message"); logger.Error("This is an Error message"); logger.Fatal("This is a Fatal error message");
有NLog
任何全球事件,所以有可能使用它并填充RichTextBox
?
谢谢!
我们必须安装 https://www.nuget.org/packages/NLog.Windows.Forms
使用完毕后 NLog.Windows.Forms;
最后添加代码
private void FormMain_Load(object sender, EventArgs e) { NLog.Windows.Forms.RichTextBoxTarget target = new NLog.Windows.Forms.RichTextBoxTarget(); target.Name = "RichTextBox"; target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}"; target.ControlName = "richTextBoxMainLog"; target.FormName = "FormMain"; 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); }
您还需要配置NLog目标.
下载示例项目并测试它https://github.com/NLog/NLog.Windows.Forms