在你想知道之前,我已经读过了 Similar Topic 并尝试重命名' Common.Logging.Log4Net'在工厂适配器 - app.config中的标签,但这对我没有帮助。我还试着注释了startup-Tag和runtime-Tag。
所以我已经下载了Common.Logging.LogNet1213.3.3.1 NuGet包。现在在我的项目中有包
根据Documentation,我填写了我的app.config,现在看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.15.0" newVersion="1.2.15.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger name="MyApp.DataAccessLayer">
<level value="DEBUG" />
</logger>
</log4net>
</configuration>
用于尝试Common.Logging的我的C#控制台应用程序如下所示:
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonLogging
{
class Program
{
static void Main(string[] args)
{
ILog log = LogManager.GetLogger<Program>();
log.Debug("qwerty");
}
}
}
异常出现在ILog日志的初始化行中,这是消息:
未处理的类型&#39; Common.Logging.ConfigurationException&#39;发生在Common.Logging.dll
中其他信息:无法从配置部分获取Common.Logging的配置&#39; common / logging&#39;。
答案 0 :(得分:0)
您获得ConfigurationException
,因为您的配置无效,特别是<configSections>
元素的位置:
每个配置文件只允许一个
<configSections>
元素,如果存在必须是根元素的第一个子元素
所以你的配置文件应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!-- everything else -->