我有这个工作,但我尝试更新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文件的答案,至少我找到了以下解决方法:
我从这里调整了答案:(我不喜欢着色选择)
Display NLog trace in RichTextBox
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 );
}