2010-02-19 97 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 

固定它。