使用 Castle.Facilities.Logging 和 log4net 进行日志记录 [英] Logging with Castle.Facilities.Logging and log4net
问题描述
我正在尝试为 Castle Windsor 工作进行 log4net 集成.我使用 ILogger
类型的公共属性编写了我的类,并在我的 app.config 中采用了如下配置.
I'm trying to get log4net integration for Castle Windsor working. I wrote my class with an public property of type ILogger
and took the configuration in my app.config like following.
<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 配置吗?
In my eyes this should be working, but it doesn't. When I set loggingApi="console"
it logs correctly. When I change it to log4net it does nothing. The log4net configuration was taken from another project where the block is working. What do I have to do that the log file is used? Must there be a special log4net configuration?
感谢您的提示
鲍里斯
推荐答案
将您的 log4net 配置移动到单独的文件 log4net.config,然后从设施配置中引用该文件:
Move your log4net configuration to a separate file log4net.config, then refer that file from the facility configuration:
<facility id="loggingfacility" configfile="log4net.config" loggingapi="log4net" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>
如果您想在 app.config 的某个部分中包含 log4net 配置:
If you want to have your log4net configuration in a section of your app.config:
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"/>
这篇关于使用 Castle.Facilities.Logging 和 log4net 进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!