'NLog: Does the FormControl target really exist?

According to the documentation, NLog offers a FormControl target that will write log messages into the Text property of a control on a Windows Form. However, when I add a FormControl target to my configuration, I get an exception telling me that no target exists named "FormControl". I did download the NLog.Windows.Forms package and include a reference to the DLL in my project.

Here's the configuration:

<?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"
      throwExceptions="true">

  <!-- 
  See https://github.com/nlog/nlog/wiki/Configuration-file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!--
    <target xsi:type="File" name="FileTarget" fileName="${basedir}/NLogger_4_1_2.log"
            layout="${date} ${uppercase:${level}} ${message}" />
    -->
    <target name="AsyncTarget" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
      <target xsi:type="File" name="FileTarget1" fileName="${basedir}/NLogger_4_1_2.log"
              layout="${date} ${uppercase:${level}} ${message}" />
    </target>
    <target xsi:type="File" name="ReportTarget" fileName="${basedir}/NLogger_4_1_2_report.log"
            layout="${date} ${uppercase:${level}} ${message}" />
    <target xsi:type="FormControl"
            name="FormControlTarget"
            layout="${message}"
            append="true"
            controlName="TextBox1"
            formName="Form1" />

  </targets>

  <rules>
    <logger name="FileLogger" minlevel="Trace"
            writeTo="AsyncTarget" />
    <logger name="ReportLogger" minlevel="Trace"
            writeTo="ReportTarget" />
    <logger name="FormLogger" minlevel="Trace"
            writeTo="FormControlTarget" />
  </rules>
</nlog>


Solution 1:[1]

I found this question in 2022 trying to get this working in VS2022. The answer is you need the NLog.Windows.Forms NuGet package installed.

And it is 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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rolf Kristensen