首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NLog和Common.Logging噩梦

NLog和Common.Logging噩梦
EN

Stack Overflow用户
提问于 2014-05-06 20:09:06
回答 4查看 10.9K关注 0票数 5

所以我尽我所能让这两个人一起玩。

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

我的配置如下:

代码语言:javascript
复制
<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的单独文件中:

代码语言:javascript
复制
<?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版本。不管我做什么,我都有例外:

代码语言:javascript
复制
{"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)"}

我遗漏了什么?这不应该很难找到工作。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-06 21:37:44

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

票数 3
EN

Stack Overflow用户

发布于 2014-05-15 18:56:03

Common.Logging.NLog20 -版本2.2.0.0

Common.Logging -版本2.2.0.0

NLog -版本2.1.0.0

配置如下所示:

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2017-07-05 23:32:53

这里有一个暗地里的方法,可以帮你省下很多麻烦。Common.Logging.NLogXX附带的适配器,NLogLoggerFactoryAdapter,除了为您设置NLog之外,没有什么特别的功能。这是大多数人都可以自己做的事情。

换句话说,如果您已经设置了NLog,那么您需要做的就是将Common.Logging连接到NLog。这可以通过编写一个简单的工厂来完成,该工厂将创建如下所示的NLog记录器:

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23504018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档