我已经做到了这一点,但是我尝试更新nlog并使用nuget包,包括NLog.Windows.Forms。
现在,除了内部例外,我还得到了NLog.NLogConfigurationException:
找不到目标:“RichTextBox”
该项目同时引用: NLog和NLog.Windows.Forms
来自packages.config:
<package id="NLog" version="4.3.4" targetFramework="net46" />
<package id="NLog.Config" version="4.3.4" targetFramework="net46" />
<package id="NLog.Schema" version="4.3.4" targetFramework="net46" />
<package id="NLog.Windows.Forms" version="4.2.3" targetFramework="net46" />来自nlog.config
<target name="rtb" xsi:type="RichTextBox" controlName="RichTextBox1" formName="NewForm"
useDefaultRowColoringRules="true" layout="[${date}] [${level:uppercase=true}] [${logger}] ${message}" />..。
<logger name="*" minlevel="Trace" writeTo="file,rtb" />添加内部日志似乎没有提供更多的信息:
...
...
2016-06-03 06:17:23.9385 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2016-06-03 06:17:23.9550 Info Adding target File Target[file]
2016-06-03 06:17:23.9550 Debug Registering target file: NLog.Targets.FileTarget
2016-06-03 06:17:23.9550 Error Error in Parsing Configuration File. Exception: NLog.NLogConfigurationException: Exception occurred when loading configuration from C:\Users\Derek.Morin\Documents\Visual Studio 2010\Projects\ScriptCode\ScriptCode.ConvertedToC#\bin\x86\Debug\NLog.config ---> System.ArgumentException: Target cannot be found: 'RichTextBox'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
2016-06-03 06:17:23.9700 Error Error has been raised. Exception: System.ArgumentException: Target cannot be found: 'RichTextBox'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)如果我无法找到如何使用nlog.config文件的答案,至少我找到了以下解决办法:
我从这里改编了答案:(我不喜欢颜色的选择)
private void SetupRichTextBoxLogger()
{
NLog.Windows.Forms.RichTextBoxTarget target = new NLog.Windows.Forms.RichTextBoxTarget();
target.Name = "RichTextBox";
target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}";
target.ControlName = nameof(this.RichTextBox1);
target.FormName = nameof(NewForm);
target.AutoScroll = true;
target.MaxLines = 0;
target.UseDefaultRowColoringRules = true;
AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper();
asyncWrapper.Name = "AsyncRichTextBox";
asyncWrapper.WrappedTarget = target;
SimpleConfigurator.ConfigureForTargetLogging( asyncWrapper, LogLevel.Trace );
}发布于 2022-04-08 11:14:18
除了安装nuget包NLog.Windows.Forms (请记住使用v4.6或更高版本,以及NLog 5.0)
然后,还建议更新NLog.config以包含<extensions>中的NLog.Windows.Forms-程序集
<?xml version="1.0" encoding="utf-8" ?>
<nlog>
<extensions>
<add assembly="NLog.Windows.Forms"/>
</extensions>
...
</nlog>发布于 2022-06-07 16:04:03
FWIW,在升级到NLog 5并删除不推荐的NLog.Config之后,我遇到了这个问题。在使用 NLog.Config恢复到4.7.15 NLog.Config时,问题不再发生。
在我的例子中,安装NLog.Windows.Forms并没有帮助。
https://stackoverflow.com/questions/37600639
复制相似问题