本帖最后由 icysky0605 于 2022-7-28 11:10 编辑
第一,logger要在FMain_Load中初始化,因为要等控件创建完毕。
第二,要自行注册一个Target,如下,当然,只要初始化一次,整个程序都共用的
ConfigurationItemFactory.Default.Targets.RegisterDefinition("RichTextBox", typeof(RichTextBoxTarget));
logger = LogManager.GetCurrentClassLogger();
下面是我的配置,具体每个项目是啥意思,可以去看官方文档,最后,我的NLog用的是5,最新版的
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Error" internalLogFile="${basedir}/Logs/interna.log">
<targets async="true">
<target name="AsyncRichTextBox"
xsi:type="RichTextBox"
allowAccessoryFormCreation ="false"
controlName="richLog"
formName="FMain"
maxLines="5000"
autoScroll="true"
useDefaultRowColoringRules="false"
layout="${longdate} ${message}">
<row-coloring condition="level = LogLevel.Trace" fontColor="Lavender"/>
<row-coloring condition="level = LogLevel.Debug" fontColor="DarkGoldenrod"/>
<row-coloring condition="level = LogLevel.Info" fontColor="LimeGreen"/>
<row-coloring condition="level = LogLevel.Warn" fontColor="DarkOrange"/>
<row-coloring condition="level = LogLevel.Error" fontColor="Red"/>
<row-coloring condition="level = LogLevel.Fatal" fontColor="HotPink"/>
<word-coloring fontColor="LightSteelBlue" ignoreCase="true" regex="([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{4})" style="Regular"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="AsyncRichTextBox" />
</rules>
</nlog> |