Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog giving exception Possible explanation is lack of zero arg and single arg Common.Logging.Configuration.NameValueCollection constructors

I added NLog to my application and the test project for it. Both of them part of the same solution. From inside the application NLog works. But from test project get below exception:

Unable to create instance of type Common.Logging.NLog.NLogLoggerFactoryAdapter. Possible explanation is lack of zero arg and single arg Common.Logging.Configuration.NameValueCollection constructors

My NLog configuration is below:

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>    
    <common>
            <logging>
              <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
                <arg key="configType" value="INLINE" />
              </factoryAdapter>
            </logging>
          </common>
          <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="C:\git\foo\logs\nlog.log" internalLogLevel="Warn">
            <extensions>
              <add assembly="NLog.RollbarSharp" />
            </extensions>
            <targets>
              <target xsi:type="RollbarSharp" name="Rollbar" />
              <target xsi:type="File" name="FileLog" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${logger}|${message}${onexception:${exception:format=tostring}}" encoding="iso-8859-2" fileName="C:\git\foo\logs\foo-${shortdate}.log" archiveFileName="C:\git\foo\logs\archives\foo-${shortdate}.{#####}.log" archiveAboveSize="5000000" archiveNumbering="Sequence" concurrentWrites="true" keepFileOpen="false" />
            </targets>
            <rules>
              <logger name="*" minLevel="Trace" writeTo="FileLog" />
              <logger name="*" minLevel="Error" writeTo="Rollbar" />
            </rules>
          </nlog>

packages.config file

<packages>
  <package id="Common.Logging" version="2.3.1" targetFramework="net45" />
  <package id="Common.Logging.NLog20" version="2.3.1" targetFramework="net45" />
  <package id="NLog" version="3.1.0.0" targetFramework="net45" />
  <package id="NLog.RollbarSharp" version="0.1.2.0" targetFramework="net45" />
  <package id="RollbarSharp" version="0.3.1.0" targetFramework="net45" />
</packages>

like image 537
PUG Avatar asked Dec 10 '14 16:12

PUG


2 Answers

When I took a closer look there was an inner exception which complained an nlog 2.0.0 dll was not found. Found a solution as follows

  1. Delete all packages related to nlog and commons logging restart application. If the packages don't get deleted by nuget edit .csproj file and .packages file manually to remove all references to Commons logging or nlog packages.
  2. Then searched for latest nlog package as by entering Common.Logging.Nlog in search text box. Then pick the latest nlog package and it will install all other needed packages for using using nlog through commons logging as a dependency. As shown below: enter image description here
  3. In your web.config update your factory adapter element In my case i selected 31 so updated it to

    <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog31"> <arg key="configType" value="INLINE" /> </factoryAdapter>

enter image description here

P.S. If you are getting error The configuration section cannot contain a CDATA or text element. Then the problem has to be a type in your config file.

like image 106
PUG Avatar answered Oct 29 '22 12:10

PUG


I had this error after moving my Common.Logging and NLog config from one web site to another. I was missing the 'dependentAssembly' blocks from the assembly binding section of the web.config:

<assemblyBinding>
  ...
  <dependentAssembly>
    <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly>
</assemblyBinding>

Added in case this helps someone else with the same problem as me!

like image 41
Ian Grainger Avatar answered Oct 29 '22 12:10

Ian Grainger