当前位置:  开发笔记 > 编程语言 > 正文

在RichTextBox中显示NLog跟踪

如何解决《在RichTextBox中显示NLog跟踪》经验,为你挑选了1个好方法。

我想在应用程序执行的同时显示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

谢谢!



1> Developer..:

我们必须安装 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

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有