【问题标题】:Can NLog v2 be used with Common.LoggingNLog v2 可以与 Common.Logging 一起使用吗
【发布时间】:2011-08-27 01:06:33
【问题描述】:

我今天尝试将它们一起使用,但版本不匹配,因为它正在寻找 NLog v1。

Common.Logging 是否支持 NLog v2?

如果没有,有谁知道是否可以安全地使用程序集版本重定向?

【问题讨论】:

  • 您找到解决方案了吗?我自己也遇到过同样的问题,并且很难找出 Common.Logging 中所需的代码更改以使其针对 NLog 2.0 进行编译
  • @dice - 不幸的是,目前我使用的是早期版本的 NLog
  • @UpTheCreek 您是否尝试过程序集重定向?查了一下Common.Logging对NLog的使用,好像兼容NLog2。

标签: logging nlog common.logging


【解决方案1】:

您可以简单地在 app.config 或 web.config 中进行程序集重定向,通过使用 NLog2 作为日志框架,CommonLogging 将与 NLog2 一起正常工作:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

唯一的问题是,如果您想使用 CommonLogging 2.0 将 NLog 消息重定向到其他一些日志框架,则存在兼容性问题。但这是一个非常不寻常的用例。

【讨论】:

  • 这并不完全有效。当 Common.Logging.NLog 尝试加载 NLog.TargetWithLayout 时出现错误,但该类已移至 NLog.Targets.TargetWithLayout,因此加载失败。这似乎对很多人都有效,所以接下来要看看使用 MEF 是否会破坏这一点。
  • 我通过针对 NLog 2.0 编译 Common.Logging 源来完成这项工作。所需的更改非常小。只需将Common.Logging.NLog.CommonLoggingTargets 基类更改为NLog.Targets.TargetWithLayout 并将第90 行替换为log(logger, delegate { return this.Layout.Render(logEvent); }, logEvent.Exception);。不是一个很好的解决方案,但现在对我有用。
【解决方案2】:

如果您使用 Nuget 获取 Common.Logging.NLog 库,该包将下载 Common.Logging v2.0 依赖项。如果你使用 Nuget 更新Common.Logging,它会更新到 v2.1.1.0,这样可以防止Could not load file or assembly 'NLog, Version=1.0.0.505 错误。

【讨论】:

    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多