gpt4 book ai didi

NLog 和 Common.Logging 的噩梦

转载 作者:行者123 更新时间:2023-12-04 18:42:13 25 4
gpt4 key购买 nike

所以我已经尝试了我能找到的一切让这两个一起玩。

我已经安装了 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,我尝试更改绑定(bind)重定向,我尝试将 Common.Logging 更新到版本 2.2.0.0。无论我做什么,我都会得到异常(exception):
{"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)"}

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

最佳答案

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

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

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();
}

关于NLog 和 Common.Logging 的噩梦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23504018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com