Klee Klee - 1 year ago 165
C# Question

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

  • 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:

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

... and ...

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

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=, Culture=neutral,

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
file but I can if it would be beneficial.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download