2015-08-08 168 views
0

此問題被問到here,但該解決方案不是編程配置。在這種情況下,可以使用Common.Logging正確配置庫Wrapper.dll。控制檯應用程序ConsoleApplication1.exe試圖實施Log4NetLoggerFactoryAdapter以編程方式配置Log4NetLoggerFactoryAdapter(重試)

This works fine,將日誌條目從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(); 

隨着編程解決方案,我可以成功地內ConsoleApplication1使用GetLogger從適配器或者或log4net,但我不能讓日誌事件通過「Wrapper」庫中使用的記錄器傳播。

請注意,在工作正常我已經允許xml和註釋的編程調用。在不起作用我已經評論了相關的xml並實現了編程代碼。還要注意這是一個微不足道的例子。真正的應用程序正試圖使用​​一個.NET庫來實現Matlab的Common.Logging

回答

0

您創建適配器後,必須將其設置爲LogManager's Adapter

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

完美。謝謝。 – mtech