问题描述
我正在尝试为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进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!