【问题标题】:NLog and Common.Logging nightmareNLog 和 Common.Logging 的噩梦
【发布时间】:2014-05-06 20:09:06
【问题描述】:

所以我想尽一切办法让这两个人一起玩。

我已经安装了nuget包Common.Logging.NLog20,

我的配置如下:

<configSections>
    <sectionGroup name="common">
        <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog20" />
</configSections>
<common>
    <logging>
        <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
    </logging>
</common>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

我正在使用 nuget NLog.Configuration 包,因此我的 nlog 配置位于一个名为 NLog.config 的单独文件中:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  internalLogFile="nlog.ERRORS.txt" internalLogLevel="Error" >

<!-- 
See http://nlog-project.org/wiki/Configuration_file 
for information on customizing logging rules and outputs.
-->
<targets>
    <!-- add your targets here -->
    <target xsi:type="File" name="log" keepFileOpen="true"
            fileName="${basedir}/log_${date:format=yyyyMMdd}.txt"
            layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
    <target name="log_errors_memory" xsi:type="Memory"
            layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
    <target name="log_all_memory" xsi:type="Memory"
            layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
</targets>

<rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Trace" writeTo="log" />
    <logger name="*" minlevel="Trace" writeTo="log_all_memory" />
    <logger name="*" minlevel="Error" writeTo="log_errors_memory" />
</rules>
</nlog>

我尝试将 FactoryAdaptor 更改为 NLog、NLog2 和 NLog20,我尝试更改绑定重定向,我尝试将 Common.Logging 更新到版本 2.2.0.0。无论我做什么,我都会得到异常:

{"Failed obtaining configuration for Common.Logging from configuration section 'common/logging'."}

Inner Exception:
{"An error occurred creating the configuration section handler for common/logging: Type Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20, Version=2.2.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e does not implement Common.Logging.ILoggerFactoryAdapter\r\nParameter name: factoryAdapterType\r\nActual value was Common.Logging.NLog.NLogLoggerFactoryAdapter. (D:\\Development\\Code\\DotNet\\vs2013\\exe\\CommandLine\\PSVImporter\\FidessaPSVImport.Test\\bin\\Debug\\FidessaPSVImport.Test.dll.config line 17)"}

我错过了什么?这不应该这么难开始工作。

【问题讨论】:

  • 太多的痛苦收获太少。创建了我自己的简单抽象。 Goodby Common.Logging。
  • 所以我注意到我的 nlog configSection 有 NLog20 而不是 NLog,解决了这个问题,没有骰子。另外,工厂属性说是内联的,所以我删除了 NLog.config 文件并将 nlog 部分放入 appconfig。还是没有运气。
  • 派对迟到了,但对于其他登陆这里的人:&lt;arg key="configType" value="INLINE" /&gt; 条目仅当您的 NLog 配置在同一个文件中时!如果您的 NLog 配置在一个单独的文件中(如 NLog.config),那么您需要指定 &lt;arg key="configType" value="FILE" /&gt; 并指定文件名。见docs

标签: nlog common.logging


【解决方案1】:

好的,所以在完成上述所有修复后,我还必须将 Common.Logging 包更新到 v2.2.0.0,然后手动更新绑定重定向。这确实是 Common.Logging.NLog20 nuget 包的次优部署。你不应该这样做。

【讨论】:

  • 我做了你建议的一切,但我仍然收到同样的错误
  • 不幸的是,我不是 nLog/Common.Logging 专家,所以以上所有内容都是我必须提供的。我只能建议仔细检查以确保您的配置文件中没有输入错误。我将在另一个答案中为您发布我的所有版本以及我的配置片段。它对我有用,所以它应该对你有用。
  • 我能想到的唯一另一件事是权限问题(因为您无权访问您的 app.config 文件)。但是如果你在调试时遇到这个错误,我看不出这是怎么发生的。
  • 这对我有用。 Common Logging 处于毁灭状态。现在我依赖于 NLog,因为 log4net 的故事不起作用。依赖地狱......这太疯狂了! @BitFiddler 感谢分享。
【解决方案2】:

Common.Logging.NLog20 - 版本 2.2.0.0

Common.Logging - 版本 2.2.0.0

NLog - 版本 2.1.0.0

配置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    </configSections>
    <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="nlog.ERRORS.txt" internalLogLevel="Error">

        <!-- 
          See http://nlog-project.org/wiki/Configuration_file 
          for information on customizing logging rules and outputs.
        -->
        <targets>
            <!-- add your targets here -->
            <target xsi:type="File" name="log" keepFileOpen="true"
                    fileName="${basedir}/log_${date:format=yyyyMMdd}.txt"
                    layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
            <target name="log_errors_memory" xsi:type="Memory"
                    layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
            <target name="log_all_memory" xsi:type="Memory"
                    layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" />
        </targets>

        <rules>
            <!-- add your logging rules here -->
            <logger name="*" minlevel="Trace" writeTo="log" />
            <logger name="*" minlevel="Trace" writeTo="log_all_memory" />
            <logger name="*" minlevel="Error" writeTo="log_errors_memory" />
        </rules>
    </nlog>
   </value>
    <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.2.0.0" newVersion="2.2.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

【讨论】:

    【解决方案3】:

    这是一种不为人知的方法,可以为您省去很多麻烦。 Common.Logging.NLogXX 附带的适配器,NLogLoggerFactoryAdapter,除了为您设置 NLog 之外没有什么特别之处。这是大多数人可以自己做的事情。

    换句话说,如果您已经设置了 NLog,您只需将 Common.Logging 连接到 NLog。这可以通过编写一个简单的工厂来完成,该工厂将像这样创建 NLog 记录器:

    public class MyAdapter : AbstractCachingLoggerFactoryAdapter
    {
        protected override ILog CreateLogger(string name)
        {
            return (ILog)typeof(NLogLogger).GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(NLog.Logger) }, null)
                .Invoke(new object[] { NLog.LogManager.GetLogger(name) });
        }
    }
    
    public static void ConfigureLogging()
    {
        Common.Logging.LogManager.Adapter = new MyAdapter();
    }
    

    【讨论】:

      【解决方案4】:

      最新的 NLog 无需 Common.Logging。在我的情况下,我只需要执行以下操作以避免错误“无法从配置部分 'common/logging' 获取 Common.Logging 的配置”

      Uninstall-Package Common.Logging
      Uninstall-Package Common.Logging.Core
      Install-Package NLog.Config
      

      然后从不必要的配置中清除 app.config 并改用 nlog.config。就像这里描述的:http://nlog-project.org/download/

      之后我不得不重构一些代码,但它的工作方式与 common.logging 几乎相同

      LogManager.GetLogger(GetType().ToString()).Info("demo")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 2010-12-21
        • 2020-03-14
        • 2010-11-13
        相关资源
        最近更新 更多