Search code examples
.netnlogtargets

nlog Can't load custom target from referenced assembly


I've created the following custom target:

[Target("MyTarget")
public class MyTarget : TargetWithLayout

This class is defined within its own assembly, lets say MyTargets.dll (not real name). The NLog.config file has the following lines

<extensions>
    <add assembly="MyTargets, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</extensions>

A target has been defined to use this new type:

<target name="myTarget" xsi:type="MyTarget" />

A logger has been defined to use this target (omitted).

My application will successfully load the configuration if I reference the targets assembly project. If I try and reference an output DLL it will fail to load. If I programmatically add the target from the assembly (bin reference not project) then it works.

The DLLs seems to be in the right place, i.e. bin dir. The type must exist because I can reference the type in code but it seems to fall over when trying to reference the type in code.

Why not just do it in code? Well its part of a nuget package that I'm building and I want a standard configuration file distributed to all consumers of this code lib.

Any suggestions / ideas would be greatly appreciated


Solution

  • Turns out I am an idiot.

    For unrelated reasons I had decided to copy the NLog.config file to the bin directory on build, whilst keeping the original at the root project level. This has caused some sort of conflict within NLog.

    When I remove the config file from the bin directory then the configuration file at the root level is loaded correctly and my custom targets are found as expeceted. If I remove the config file from the project root level and put it in the bin directory (i.e the same level as the assembly containing the custom target) then the configuration file that is loaded will not be able to find the custom target.

    So to recap, I stopped putting the configuration file in the bin directory and left it where it is defined (the project root level)