Configuring Log4NetLoggerFactoryAdapter Programmatically (Trying Again)

时光怂恿深爱的人放手 提交于 2019-12-24 14:04:44

问题


This question was asked here, however the solution was not a programmatic configuration. In this case, a library Wrapper.dll is properly configured with Common.Logging. A console application ConsoleApplication1.exe attempts to implement a Log4NetLoggerFactoryAdapter.

This works fine, sending log entries from Wrapper.dll to the console.

The 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>

The code within ConsoleApplication1:

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

This does not work. It does not send log entries from Wrapper.dll to the console. The 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>

The code within ConsoleApplication1:

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

With the programmatic solution I can successfully use GetLogger within ConsoleApplication1 from either the adapter or from log4net, but I cannot get the log events to propagate through from the loggers used in the "Wrapper" library.

Note that in works fine I have allowed the xml and commented the programmatic invocation. In does not work I have commented the relevant xml and implemented the programmatic code. Note also that this is a trivial example. The real application is trying to use a .NET library that implements Common.Logging from Matlab.


回答1:


After you create the adapter you have to set it as the LogManager's Adapter:

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


来源:https://stackoverflow.com/questions/31899122/configuring-log4netloggerfactoryadapter-programmatically-trying-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!