9

我正在尝试让温莎城堡的 log4net 集成工作。我用类型的公共属性编写了我的类,ILogger并在我的 app.config 中进行了配置,如下所示。

<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 配置吗?

感谢您的任何提示

鲍里斯

4

6 回答 6

9

将您的 log4net 配置移动到单独的文件 log4net.config,然后从设施配置中引用该文件:

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

如果您想在 app.config 的一部分中配置 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"/>
于 2010-02-19T19:53:41.220 回答
6

您可以将 App.config 用于您的 log4net 配置,而无需创建自定义日志记录工厂。只需将 App.config 文件作为参数提供给LoggingFacility构造函数:

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

与 Mauricio Scheffer 的回答不同,默认工厂是ConfigureAndWatch. 这对我来说适用于 App.config 文件,尽管我没有在 IIS 或其他任何限制权限的东西上运行。

我在代码中这样做是因为您不能可靠地使用 Windsor Xml 配置从 App.config 加载 log4net 配置。这是因为 App.config 的位置可以在创建新的AppDomain.

使用我的解决方案意味着日志文件配置将被编译到您的代码中。但是您可以通过使用 Windsor 安装程序来配置日志记录并从 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 更改为指向不同程序集中的 Windsor Installers:

<castle>
    <installers>
        <install assembly="SomeOtherAssemblyName" />
    </installers>
</castle>
于 2011-04-14T23:33:10.200 回答
3

请注意,您可以在最近的 Castle 版本中使用以下内容:

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

这将使用 Log4net 进行日志记录并在应用程序配置文件中搜索 log4net 配置部分。

于 2013-11-10T12:33:33.243 回答
1

这是此处给出的示例的完整配置Home » MicroKernel/Windsor » 入门 » 第 1 部分 - 基础知识

<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>

我犯的错误是(正如您可以看到配置文件中注释的部分),试图通过在城堡中注册组件来再次在我的类中分配 Logger 属性。这不是必需的,因为 Castle 会自动为您执行此操作。

干杯巴达尔

于 2010-09-06T07:01:27.120 回答
0

遵循最近的 Castle 约定(如果有错误请纠正我),我以这种方式解决了它:

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

我只是调整了这里提供的方法:http: //docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx

并使用了接受配置文件参数的 UseLog4Net() 的重载。

于 2013-10-30T20:11:08.743 回答
0

我缺少以下 NuGet 包。

Castle.Windsor-log4net

Install-Package Castle.Windsor-log4net

修复。

于 2017-02-02T17:39:20.173 回答