温莎城堡在哪里以及如何建立伐木设施

vdh*_*ant 10 .net c# logging castle-windsor inversion-of-control

我对温莎城堡很新,我正在研究伐木设施的进出口.这似乎相当令人印象深刻,但我唯一无法解决的是Windsor在我的类上设置Logger属性.如下所示,如果尚未设置类,则将Logger设置为nullLogger,但是当Resolve完成运行时,将设置Logger属性.

private ILogger logger;

public ILogger Logger
{
    get
    {
        if (logger == null) 
            logger = NullLogger.Instance;
        return logger;
    }
    set { logger = value; }
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道windsor如何以及在哪里设置我的Logger属性.

干杯安东尼

Tod*_*odd 12

记录器由记录工具设置,该工具<facilities>位于配置部分.例如,使用log4net你的app或web.config看起来像这样:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
    </configSections>
<Configuration>

<castle>

    <facilities>
        <facility id="loggingfacility" 
             type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" 
             loggingApi="log4net" 
             configFile="logging.config" />
    </facilities>

</castle>
</configuration>
Run Code Online (Sandbox Code Playgroud)


UpT*_*eek 11

初始化windsor时,您也可以以编程方式配置它(例如,从global.asax.cs):

container.AddFacility("logging",  new LoggingFacility(LoggerImplementation.Log4net));
Run Code Online (Sandbox Code Playgroud)

您当然可以选择任何记录器实施方案.

只要windsor实例化任何期望记录器的类,这将被连线.我不会把它放在构造函数中,因为它是一个交叉问题 - 更好的做法就像你在我看来建议的那样.您可以稍微简化一下:

    private ILogger logger = NullLogger.Instance;
    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }
Run Code Online (Sandbox Code Playgroud)