1

I had this working, but I tried to update nlog and use the nuget packages, including NLog.Windows.Forms.

Now I am getting NLog.NLogConfigurationException with the inner exception:

Target cannot be found: 'RichTextBox'

The project references both: NLog and NLog.Windows.Forms

from 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" />

from 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" />

Adding internal logging didn't seem to give much more information:

...
...
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)

If I can't get an answer for how to get it working with the nlog.config file, at least I found the following workaround:

I adapted the answer from here: ( I didn't like the colouring choices )

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 );
   }
Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58

2 Answers2

1

Besides having the nuget-package NLog.Windows.Forms installed (Remember to use v4.6 or newer, together with NLog 5.0)

Then it is also recommended to update NLog.config to include NLog.Windows.Forms-assembly in <extensions>:

<?xml version="1.0" encoding="utf-8" ?>
<nlog>
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions> 

  ...
</nlog>
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
0

FWIW, I had this problem after upgrading to NLog 5 and removing the deprecated NLog.Config. Upon reverting to 4.7.15 with NLog.Config, the problem no longer occurred.

In my case, having NLog.Windows.Forms installed did not help.

Nick
  • 1
  • [NLog.Windows.Forms](https://www.nuget.org/packages/NLog.Windows.Forms/) v4.6 should work just fine with NLog 5.0. You are wellcome to create an issue at https://github.com/NLog/NLog.Windows.Forms where you provide the `NLog.config` and the output from the [NLog InternalLogger](https://github.com/NLog/NLog/wiki/Internal-Logging) at Debug-Level. – Rolf Kristensen Jun 07 '22 at 18:00