在RichTextBox中显示NLog跟踪 [英] Display NLog trace in RichTextBox

查看:151
本文介绍了在RichTextBox中显示NLog跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在应用执行时同时在RichTextBox中显示NLog跟踪

I whant to display NLog trace into RichTextBox at the same time when app executes

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?

Has NLog any global event so it is possible to use it and populate 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目标.

Also you need to configure NLog target.

 <target xsi:type="RichTextBox"
            name="target2"
            layout="${message} ${rtb-link:link text in config}"

            formName="Form1"
            ControlName="richTextBoxMainLog"
            autoScroll="true"
            maxLines="20"

            allowAccessoryFormCreation="false"
            messageRetention="OnlyMissed"

            supportLinks="true"

            useDefaultRowColoringRules="true" />

下载示例项目并对其进行测试 https://github.com/NLog/NLog. Windows.Forms

Download the sample project and test it https://github.com/NLog/NLog.Windows.Forms

这篇关于在RichTextBox中显示NLog跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆