2010-02-19 112 views
9

我試圖讓Castle Windsor的log4net集成工作。我用類型爲​​的公共屬性編寫了我的類,並在我的app.config中進行了如下配置。與Castle.Facilities.Logging和log4net記錄

<configuration> 
    <configsections> 
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
    </configsections> 

    <castle> 
    <facilities> 
     <facility id="logging" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" loggingApi="log4net" /> 
    </facilities> 
    <components> 
     <component id="form1" type="WinFormsActiveRecordSample.Form1, WinFormsActiveRecordSample" /> 
    </components> 
    </castle> 
    <log4net> 
    <root> 
     <level value="ALL" /> 
     <appender-ref ref="FileAppender" /> 
    </root> 
    <appender name="FileAppender" type="log4net.Appender.FileAppender"> 
     <file value="main.log" /> 
     <appendToFile value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date{dd.MM.yy HH:mm:ss} %-5level %logger - %message%newline" /> 
     </layout> 
    </appender> 
    </log4net> 
</configuration> 

在我眼中,這應該是工作,但它沒有。當我設置loggingApi="console"它正確記錄。當我將其更改爲log4net時,它什麼都不做。 log4net配置是從塊正在工作的另一個項目中獲取的。我需要做什麼,使用日誌文件?必須有一個特殊的log4net配置?

感謝您的任何提示

鮑里斯

+0

它終於奏效了嗎? – 2010-02-24 13:44:08

+0

在我的代碼稍作修改後,是的它:) :) – Booser 2010-02-24 14:11:26

回答

9

移動你的log4net的配置到一個單獨的文件log4net.config,然後從設備配置,請參閱該文件:

<facility id="loggingfacility" configfile="log4net.config" loggingapi="log4net" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/> 

如果你想擁有您的app4config的一部分中的log4net配置:

public class MyLog4NetFactory: Log4netFactory { 
    public MyLog4NetFactory() { 
     XmlConfigurator.Configure(); 
    } 

    public override ILogger Create(String name) { 
     ILog log = LogManager.GetLogger(name); 
     return new Log4netLogger(log.Logger, this); 
    } 

    public override ILogger Create(String name, LoggerLevel level) { 
     throw new NotSupportedException("Logger levels cannot be set at runtime. Please review your configuration file."); 
    } 
} 

然後註冊機構爲:

<facility 
    id="loggingfacility" 
    loggingapi="custom" 
    customLoggerFactory="[fully qualified type name of MyLog4NetFactory]" 
    type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/> 
+0

這就是我想到的方式,但我喜歡將所有配置都放在一個地方的想法。 Ithink我必須看看工廠,以獲得從app.config加載配置的方式...但是,當它的唯一日誌配置是靜態的大部分時間... 感謝您的提示 – Booser 2010-02-22 13:52:08

+0

好奇,必須'configfile =「...」* *不是*應用程序配置? @Boris如果你指定'[application.name] .exe.config',會發生什麼? – 2010-02-22 21:11:35

+0

@johnnyG:不會工作 – 2010-02-22 22:25:37

1

這是樣品這裏Home » MicroKernel/Windsor » Getting Started » Part 1 - The basics

<configuration> 
    <configSections> 
    <section 
     name="castle" 
     type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> 
    </configSections> 


    <castle> 
    <facilities> 
    <facility 
     id="logging" 
     type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" 
     loggingApi="log4net" 
     configFile="D:\\Backup-E\\My Work\\.NET\\CastleWindsorPOC\\CastleWindosorApp\\CastleWindosorApp\\Log4Net.xml" /> 
    </facilities> 
    <components> 
     <component 
      id="httpservicewatcher" 
      type="CastleWindosorApp.HttpServiceWatcher, CastleWindosorApp" > 
     <parameters> 
      <notifiers> 
      <array> 
       <item>${email.notifier}</item> 
       <item>${alarm.notifier}</item> 
      </array> 
      </notifiers> 
      <Url>test url</Url> 
      <!--<Logger>${logger.component}</Logger>--> 
     </parameters> 
     </component> 

     <component 
      id="email.notifier" 
      service="CastleWindosorApp.IFailureNotifier, CastleWindosorApp" 
      type="CastleWindosorApp.EmailFailureNotifier, CastleWindosorApp" /> 

     <component 
      id="alarm.notifier" 
      service="CastleWindosorApp.IFailureNotifier, CastleWindosorApp" 
      type="CastleWindosorApp.AlarmFailureNotifier, CastleWindosorApp" /> 
    <!--<component 
      id="logger.component" 
      service="Castle.Core.Logging.ILogger, Castle.Core" 
      type="Castle.Services.Logging.Log4netIntegration.Log4netLogger, Castle.Services.Logging.Log4netIntegration" />--> 
     <component 
      id="form.component" 
      type="CastleWindosorApp.Form1, CastleWindosorApp" /> 


    </components> 

    </castle> 

的錯誤,我所做的就是(你可以看到在配置文件中所註釋的部分)給出的整個配置,試圖通過在城堡中註冊組件來再次在我的課程中分配記錄器屬性。這不是必需的,因爲Castle會自動爲您執行此操作。

乾杯 巴達爾

6

您可以使用App.config中爲您的log4net的配置,而無需創建自定義日誌工廠。只需提供App.config文件作爲參數傳遞給LoggingFacility構造:

container 
    .AddFacility("logging", 
     new LoggingFacility(LoggerImplementation.Log4net, 
      System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) 
     ) 

與毛裏西奧·雅伯的回答,默認出廠不ConfigureAndWatch。這適用於我的App.config文件,儘管我沒有在IIS上運行或限制權限的任何其他功能。

我在代碼中這樣做是因爲您無法可靠地使用Windsor Xml配置從App.config加載log4net配置。這是因爲創建新的AppDomain時可以修改App.config的位置。

使用我的解決方案意味着日誌文件配置將被編譯到您的代碼中。但是你可以通過使用溫莎安裝配置日誌緩解這種,從App.config文件中指定的安裝程序(或安裝組件):

public class LoggingInstaller : IWindsorInstaller 
{ 
    public void Install(IWindsorContainer container, IConfigurationStore store) 
    { 
     container 
      .AddFacility("logging", 
       new LoggingFacility(LoggerImplementation.Log4net, 
        System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) 
       ); 
    } 
} 

...

<castle> 
    <installers> 
     <install assembly="MyAssemblyName" /> 
    </installers> 
</castle> 

如果將來(也許在你的測試用例),你必須從不同的文件中加載日誌配置,而不能或不想重新編譯,只需更改XML指向溫莎安裝程序在不同的裝配:

<castle> 
    <installers> 
     <install assembly="SomeOtherAssemblyName" /> 
    </installers> 
</castle> 
0

繼最近城堡公約(糾正我,如果錯了),我已經解決了這種方式:

using Castle.Facilities.Logging; 
using Castle.MicroKernel.Registration; 
using Castle.MicroKernel.SubSystems.Configuration; 
using Castle.Windsor; 

namespace EVRM.Aspects.Container.Installers 
{ 
    public class LoggingInstaller : IWindsorInstaller 
    { 
     public void Install(IWindsorContainer container, IConfigurationStore store) 
     { 
      container.AddFacility<LoggingFacility>(f => f.UseLog4Net(System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)); 
     } 
    } 
} 

我只是適應的方法provid在這裏編輯:http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx

並使用UseLog4Net()接受配置文件參數的重載。

3

注意,您可以使用下面的最近版本的城堡:

container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithAppConfig()); 

這將使用log4net的日誌記錄和搜索應用程序配置文件中log4net的配置部分。

0

我錯過了下面的NuGet包。

Castle.Windsor-log4net的

Install-Package Castle.Windsor-log4net 

固定它。