jde*_*per 22 c# log4net common.logging
我正在尝试使用以下日志记录程序集配置控制台应用程序:
如果以编程方式配置记录器,那么一切正常:
NameValueCollection properties = new NameValueCollection(); properties["showDateTime"] = "true";
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用以下配置文件启动它,它会爆炸:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<arg key="configType" value="FILE-WATCH"/>
<arg key="configFile" value="~/Log4NET.xml"/>
</factoryAdapter>
</logging>
</common>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这些是相关的错误消息:
{"Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'."}
{"Failed obtaining configuration for Common.Logging from configuration section 'common/logging'."}
Run Code Online (Sandbox Code Playgroud)
它似乎无法解析我的配置文件,是否有人知道正确的格式应该是什么,或者是其他错误的东西?我使用官方文档创建了配置文件.
Isa*_*avo 23
我也有这个(或相关的)问题,经过半天的错误搜索和调试后,我将其缩小到配置问题.
异常与OP和内部异常相同,其中的一些内容未能找到Common.Logging.Log4Net
(FileNotFoundException
).
对于Common.Logging.Log4Net1211 NuGet包,它们似乎已将程序集名称重命名为Common.Logging.Log4Net1211而不是简单的Common.Logging.Log4Net.这意味着app.config
您需要引用这个新的程序集名称:
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">
这是app.config的整个常用/日志记录部分供参考:
<common>
<logging>
<!-- Notice that it's Log4net1211 -->
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="~/Log4Net-MAIN.config" />
</factoryAdapter>
</logging>
</common>
Run Code Online (Sandbox Code Playgroud)
Jer*_*oen 12
您的应用程序存在两个问题(我下载的问题):
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
请注意log4net-section被声明两次?删除第一个.
无法加载文件或程序集'log4net,Version = 1.2.11.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
我从log4net网站下载了log4net 1.2.11.0,解压缩它,解锁了dll并替换了你示例中的log4net,它似乎工作正常.