c# - NLog 给出异常可能的解释是缺少零参数和单个参数 Common.Logging.Configuration.NameValueCollection 构造函数

标签 c# logging nuget nlog apache-commons-logging

我将 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。找到解决方法如下

  1. 删除所有与 nlog 和公共(public)日志记录重启应用程序相关的包。如果包没有被 nuget 删除,请手动编辑 .csproj 文件和 .packages 文件以删除对 Commons 日志记录或 nlog 包的所有引用。
  2. 然后通过输入 Common.Logging.Nlog 搜索最新的 nlog 包在搜索文本框中。然后选择最新的 nlog 包,它将安装所有其他需要的包,以便通过公共(public)日志记录作为依赖项使用 nlog。如下所示: enter image description here
  3. 在你的web.config更新您的工厂适配器元素在我的例子中,我选择了 31,所以将其更新为

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

enter image description here

附言如果您收到错误 The configuration section cannot contain a CDATA or text element.那么问题必须是您的配置文件中的一个类型。

关于c# - NLog 给出异常可能的解释是缺少零参数和单个参数 Common.Logging.Configuration.NameValueCollection 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27406100/

相关文章:

c# - AutoMapper:领域模型和 View 模型之间的双向深度映射

java - 将 Eclipse Tomcat 日志重定向到控制台(没有 ConcoleAppender)

java - Eclipse - 将所有记录器输出重定向到控制台

c# - 在 WinRT 应用程序中处理 2、3、4、5 个手指敲击、双击和按住手势

c# - virtual 关键字对 Entity Framework 5 中的实体属性有什么影响?

java - 如何查询 Appengine Logs API 以获取所有版本应用程序的日志?

NuGet 包还原无法使用自定义源提示输入凭据

visual-studio-2010 - 通过单个命令通过 Nuget 安装多个包

dependency-injection - IoC : ProjectReference with ReferenceOutputAssembly = false, 但仍然想要 nuget 包

c# - Avalon 中的 BraceFolding 编辑