以编程方式配置Log4NetLoggerFactoryAdapter(重试) [英] Configuring Log4NetLoggerFactoryAdapter Programmatically (Trying Again)

查看:278
本文介绍了以编程方式配置Log4NetLoggerFactoryAdapter(重试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处提出了此问题,但是解决方案不是编程配置.在这种情况下,库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();

使用编程解决方案,我可以从适配器或log4netConsoleApplication1中成功使用GetLogger,但是我无法从包装器"库中使用的记录器中传播日志事件. /p>

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

解决方案

在创建适配器后,必须将其设置为日志管理的适配器:

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

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.

解决方案

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

这篇关于以编程方式配置Log4NetLoggerFactoryAdapter(重试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆