1

这个问题在这里被问到,但是解决方案不是程序化配置。在这种情况下,库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();

使用编程解决方案,我可以从适配器或从成功使用GetLoggerConsoleApplication1log4net我无法从“包装器”库中使用的记录器中传播日志事件。

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

4

1 回答 1

0

创建适配器后,您必须将其设置为LogManager 的适配器

var adapter = new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
Common.Logging.LogManager.Adapter = adapter;
var c1 = new Wrapper();
于 2015-08-10T23:17:27.457 回答