使用Castle.Facilities.Logging和log4net进行日志记录

Boo*_*ser 9 logging log4net castle-windsor log4net-configuration

我正在努力让Castle Windsor的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>
Run Code Online (Sandbox Code Playgroud)

在我看来,这应该是有效的,但事实并非如此.当我loggingApi="console"正确设置日志时.当我将其更改为log4net时,它什么也没做.log4net配置来自块正在运行的另一个项目.我该怎么做才能使用日志文件?必须有特殊的log4net配置吗?

谢谢你的任何提示

鲍里斯

Mau*_*fer 9

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

<facility id="loggingfacility" configfile="log4net.config" loggingapi="log4net" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>
Run Code Online (Sandbox Code Playgroud)

如果您想在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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将设施注册为:

<facility 
  id="loggingfacility" 
  loggingapi="custom" 
  customLoggerFactory="[fully qualified type name of MyLog4NetFactory]" 
  type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>
Run Code Online (Sandbox Code Playgroud)


Mer*_*ham 6

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

container
    .AddFacility("logging",
        new LoggingFacility(LoggerImplementation.Log4net,
            System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        )
Run Code Online (Sandbox Code Playgroud)

与Mauricio Scheffer的答案不同,默认工厂有ConfigureAndWatch.虽然我没有在IIS或其他任何限制权限的操作上运行,但对我来说这对App.config文件来说效果很好.

我在代码中执行此操作是因为您无法可靠地使用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)
                );
    }
}
Run Code Online (Sandbox Code Playgroud)

...

<castle>
    <installers>
        <install assembly="MyAssemblyName" />
    </installers>
</castle>
Run Code Online (Sandbox Code Playgroud)

如果将来(可能在您的测试用例中)您必须从其他文件加载日志记录配置,并且不能或不想重新编译,只需将Xml更改为指向另一个程序集中的Windsor安装程序:

<castle>
    <installers>
        <install assembly="SomeOtherAssemblyName" />
    </installers>
</castle>
Run Code Online (Sandbox Code Playgroud)