首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nlog richtextbox -找不到目标:'RichTextBox‘

nlog richtextbox -找不到目标:'RichTextBox‘
EN

Stack Overflow用户
提问于 2016-06-02 19:34:39
回答 2查看 1.2K关注 0票数 1

我已经做到了这一点,但是我尝试更新nlog并使用nuget包,包括NLog.Windows.Forms。

现在,除了内部例外,我还得到了NLog.NLogConfigurationException:

找不到目标:“RichTextBox”

该项目同时引用: NLog和NLog.Windows.Forms

来自packages.config:

代码语言:javascript
复制
  <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

代码语言:javascript
复制
<target name="rtb" xsi:type="RichTextBox" controlName="RichTextBox1" formName="NewForm"
        useDefaultRowColoringRules="true" layout="[${date}] [${level:uppercase=true}] [${logger}] ${message}" />

..。

代码语言:javascript
复制
<logger name="*" minlevel="Trace" writeTo="file,rtb" />

添加内部日志似乎没有提供更多的信息:

代码语言:javascript
复制
...
...
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文件的答案,至少我找到了以下解决办法:

我从这里改编了答案:(我不喜欢颜色的选择)

在NLog中显示RichTextBox跟踪

代码语言:javascript
复制
   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 );
   }
EN

回答 2

Stack Overflow用户

发布于 2022-04-08 11:14:18

除了安装nuget包NLog.Windows.Forms (请记住使用v4.6或更高版本,以及NLog 5.0)

然后,还建议更新NLog.config以包含<extensions>中的NLog.Windows.Forms-程序集

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<nlog>
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions> 

  ...
</nlog>
票数 1
EN

Stack Overflow用户

发布于 2022-06-07 16:04:03

FWIW,在升级到NLog 5并删除不推荐的NLog.Config之后,我遇到了这个问题。在使用 NLog.Config恢复到4.7.15 NLog.Config时,问题不再发生。

在我的例子中,安装NLog.Windows.Forms并没有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37600639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档