Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Logging Could Not Load Type NLog.TargetWithLayout

I've got a Wpf .net 4.0 C# project that I am attempting to migrate from using Log4Net to using NLog as the logging library behind the Common.Logging Façade. I had expected this to be an easy task, but you know what they say, Nothing is ever easy.

I have used NuGet to:

  • Download NLog at version 2.0.1.2.
  • Download Common.Logging.NLog at version 2.0.0.
  • Download Common.Logging.NLog20 at version 2.1.2.
  • Updated Common Logging from version 2.0.0 to version 2.1.2 using NuGet.

In the app.config file I have:

<common>
    <logging>
        <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog">
            <arg key="configType" value="FILE" />
            <arg key="configFile" value="~/NLog.config" />
        </factoryAdapter>
    </logging>
</common>

... and ...

  <dependentAssembly>
    <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.1.0" newVersion="2.0.1.0" />
  </dependentAssembly>

When I run the application I get an error stating

'The invocation of the constructor on type 'NameSpace.Shell.AppBootstrapper' that matches the specified binding constraints threw an exception.' Line number '8' and line position '18'.

The inner exception is:

{"Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."}

Said Loader exception, there is one only, is:

{"Could not load type 'NLog.TargetWithLayout' from assembly 'NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'.":"NLog.TargetWithLayout"}

Has anyone overcome this problem or have a work around to get NLog working with Common.Logging?

In the interests on not posting to long of a question I have not included the NLog.config file but I can if it would be beneficial.

like image 880
Klee Avatar asked May 27 '13 04:05

Klee


2 Answers

After alot of looking around after I asked this question, and I did alot of searching before I even asked this question. I resorted to trying assorted things and hoping that they might work.

What I found was that the Common.Logging.NLog.NLogLoggerFactoryAdapter that I was using was using was coming from the Common.Logging.Nlog dll and that dll has a refrence to NLog 1.0.0.505 which uses the old position for the NLog.TargetWithLayout class.

I removed the Common.Logging.Nlog package with NuGet and changed the reference in the app.config file to:

<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">

This then uses the correct class which is found in the NLog 2.0.1.2package and allows NLog, and the application to boot up.

I hope this helps anyone else who stumbles across this problem. I couldn't find anyone else documenting how to deal with it.

like image 199
Klee Avatar answered Sep 21 '22 14:09

Klee


Probably you have installed Common.Logging.NLog NuGet package instead of Common.Logging.Nlog20. Check your nuget reference it can be confused because of version 2 of CL and version 2 of NLog. What you apparently have is version 2 of CL but NLogLoggerFactoryAdapter for NLog version 1.

like image 35
skalinkin Avatar answered Sep 22 '22 14:09

skalinkin