以编程方式配置Log4NetLoggerFactoryAdapter(再试一次)

时间:2015-08-08 22:18:45

标签: log4net log4net-configuration common.logging

这个问题被问到here,但解决方案不是程序化配置。在这种情况下,库Wrapper.dll已正确配置Common.Logging。控制台应用程序ConsoleApplication1.exe尝试实施Log4NetLoggerFactoryAdapter

正常,将Wrapper.dll的日志条目发送到控制台。

app.config

<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.Log4Net1211">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</common>
<log4net>... a valid ConsoleAppender ..</log4net>

ConsoleApplication1中的代码:

//var properties = new Common.Logging.Configuration.NameValueCollection();
//properties.Add("configType", "INLINE");
//var adapter = new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
var c1 = new Wrapper();

不起作用。它不会将Wrapper.dll的日志条目发送到控制台。 app.config

<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.Log4Net1211">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging> 
</common> -->
<log4net>... a valid ConsoleAppender ..</log4net>

ConsoleApplication1中的代码:

var properties = new Common.Logging.Configuration.NameValueCollection();
properties.Add("configType", "INLINE");
var adapter = new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
var c1 = new Wrapper();

使用程序化解决方案,我可以在GetLogger内从适配器或ConsoleApplication1成功使用log4net,但是我无法通过从中使用的记录器传播日志事件。 &#34;包装&#34;库。

请注意,在工作正常中,我已经允许xml并注释了程序化调用。在不起作用我已经评论了相关的xml并实现了程序代码。另请注意,这是一个简单的例子。真正的应用程序是尝试使用从Matlab实现Common.Logging的.NET库。

1 个答案:

答案 0 :(得分:0)

创建适配器后,必须将其设置为LogManager's Adapter

var adapter = new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
Common.Logging.LogManager.Adapter = adapter;
var c1 = new Wrapper();