Search code examples
configurationconfignlogtarget

NLog can´t find custom target?


I have a WinForm C# project where I have added NLog and a custom target. To load this target I have something like this in the NLog config :

<logger name="GeneralLogger" minlevel="Warn" writeto="customTarget"></logger>

<target name="customTarget" type="LogToService"></target>

In the nlog-internal.log I find this :

2020-05-12 11:48:16.0083 Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create target type: LogToService ---> System.ArgumentException: Target cannot be found: 'LogToService' at NLog.Config.Factory`2.CreateInstance(String itemName) at NLog.Config.LoggingConfigurationParser.CreateTargetType(String targetTypeName) --- End of inner exception stack trace ---

I have seen suggestions on adding extensions with assembly but the custom target is in the same project as where I added the NLog Nuget?

Regards


Solution

  • As it isn't in a separate assembly, you could do this:

    // NLog 4.7+
    NLog.LogManager.Setup().SetupExtensions(s =>
       s.RegisterTarget<MyNamespace.LogToService>("LogToService")
    );
    

    or this in the nlog.config (before <targets>:

      <extensions> 
        <add assembly="YourAssembly"/> 
      </extensions> 
    

    For all options, see Register your custom component