我将nlog添加到我的应用程序和测试项目中。它们都是同一解决方案的一部分。从内部应用程序nlog工作。但是从测试项目得到以下异常:
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
我的Nlog配置如下:
<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文件
<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>
当我仔细看看有一个内部异常,抱怨找不到nlog 2.0.0 dll。找到如下解决方案
Common.Logging.Nlog
来搜索最新的nlog包。然后选择最新的nlog包,它将安装所有其他所需的包,以便使用nlog通过commons logging作为依赖项。如下所示:web.config
更新你的工厂适配器元素在我的情况下,我选择31所以更新它
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog31">
<arg key="configType" value="INLINE" />
</factoryAdapter>
附:如果你得到错误The configuration section cannot contain a CDATA or text element.
然后问题必须是你的配置文件中的类型。
将Common.Logging和NLog配置从一个网站移动到另一个网站后,我遇到了这个错误。我错过了web.config的程序集绑定部分中的'dependentAssembly'块:
<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>
添加,以防这个问题帮助其他人与我相同的问题!
我以不同的方式解决了同样的错误。
返回异常消息:
ConfigurationErrorsException: Unrecognized element 'add2'.
此项目出错:
<add2 key="CommentsButtonEnd" value="]/button"/>
解决方案是:删除数字2,因为add2不是有效的配置密钥
<add key="CommentsButtonEnd" value="]/button"/>
添加万一有人和我有同样的错误。