我正在尝试让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配置?
谢谢你的任何提示
鲍里斯
发布于 2010-02-20 03:53:41
将您的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"/>发布于 2011-04-15 07:33:10
您可以使用App.config进行log4net配置,而无需创建自定义日志记录工厂。只需将App.config文件作为参数提供给LoggingFacility构造函数:
container
.AddFacility("logging",
new LoggingFacility(LoggerImplementation.Log4net,
System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
)与Mauricio Scheffer的回答不同,默认工厂执行ConfigureAndWatch。虽然我没有在IIS或任何其他限制权限的系统上运行,但这对我的App.config文件工作很好。
我之所以用代码这样做,是因为您不能可靠地使用Windsor Xml配置从App.config加载log4net配置。这是因为在创建新的AppDomain时可以修改App.config的位置。
使用my solution意味着日志文件配置将编译到您的代码中。但您可以通过使用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安装程序:
<castle>
<installers>
<install assembly="SomeOtherAssemblyName" />
</installers>
</castle>发布于 2013-11-10 20:33:33
请注意,您可以在最新的Castle版本中使用以下内容:
container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithAppConfig());这将使用Log4net进行日志记录,并在应用程序配置文件中搜索log4net配置部分。
https://stackoverflow.com/questions/2297282
复制相似问题