Мы должны установить 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.
<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
Вы это видели: http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_RichTextBoxTarget.htm –
Или https: // github.com/NLog/NLog/wiki/RichTextBox-target – stuartd
или этот http://stackoverflow.com/questions/16743804/implementing-a-log-viewer-with-wpf – Byron