简体   繁体   English

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

[英]Where & How Castle Windsor sets up logging facility

I'm fairly new to Castle Windsor and am looking into the in's and out's of the logging facility. 我对温莎城堡很新,我正在研究伐木设施的进出口。 It seems fairly impressive but the only thing i can't work out is where Windsor sets the Logger property on my classes. 这似乎相当令人印象深刻,但我唯一无法解决的是Windsor在我的类上设置Logger属性。 As in the following code will set Logger to the nullLogger if the class hasn't been setup yet but when Resolve is finished running the Logger property is set. 如下所示,如果尚未设置类,则将Logger设置为nullLogger但是当Resolve完成运行时,将设置Logger属性。

private ILogger logger;

public ILogger Logger
{
    get
    {
        if (logger == null) 
            logger = NullLogger.Instance;
        return logger;
    }
    set { logger = value; }
}

So what I am wondering is how and where windsor sets my Logger property. 所以我想知道windsor如何以及在哪里设置我的Logger属性。

Cheers Anthony 干杯安东尼

The logger is setup by the logging facility, which is in the <facilities> section of the configuration. 记录器由记录工具设置,该工具位于配置的<facilities>部分。 For example to use log4net your app or web.config would look something like this: 例如,使用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>

You can also configure this programatically when you initialise windsor (eg from your global.asax.cs): 初始化windsor时,您也可以以编程方式配置它(例如,从global.asax.cs):

container.AddFacility("logging",  new LoggingFacility(LoggerImplementation.Log4net));

You can of course choose any of the logger implimentations. 您当然可以选择任何记录器实施方案。

This this will be wired up whenever windsor instantiates any class expecting a logger. 只要windsor实例化任何期望记录器的类,这将被连线。 I wouldn't put this in the constructor as it's a cross cutting concern - better to do like you suggested in my opinion. 我不会把它放在构造函数中,因为它是一个跨领域的问题 - 在我看来你做的更好。 You can simplify it a little: 您可以稍微简化一下:

    private ILogger logger = NullLogger.Instance;
    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

Since you have a public Property with a Setter, every time you resolve your object from Windsor, it will also try to set any public properties with appropriate values from the container (in your case, an ILogger which your facility will populate into Windsor). 由于您有一个带有Setter的公共属性,因此每次从Windsor解析对象时,它还会尝试使用容器中的适当值设置任何公共属性(在您的情况下,您的工具将填充到Windsor中的ILogger)。

Meaning, if you resolve the Class from Windsor, this will be set. 这意味着,如果您从Windsor解析Class,则会设置此值。 But not if you do new Class(). 但是如果你做了新的Class()则不行。

That's atleast how I understand it. 这至少是我理解它的方式。

The other approach is to use constructors, meaning if you have a constructor named 另一种方法是使用构造函数,这意味着如果你有一个名为的构造函数

public Class(ILogger logger) it will be instantiated with ILogger as a parameter. public class(ILogger logger)将使用ILogger作为参数进行实例化。

Example: 例:

var yourClassObject = Kernel.Resolve<IClass>();

IF you don't have an interface specification (and registered as such), you will need to register your component as the concrete type if you want to resolve it using that concrete type(and not by interface). 如果您没有接口规范(并且已注册),则需要将组件注册为具体类型(如果要使用该具体类型(而不是接口)解析它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM