log4netlog4net-configurationcommon.logging

Configuring Log4NetLoggerFactoryAdapter Programmatically (Trying Again)


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.


Solution

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