简体   繁体   English

温莎城堡测井设施

[英]Castle windsor logging facility

I'm trying to remove some logging dependencies and stumbled across Castle Windsor's logging facility. 我正在尝试删除一些日志记录依赖项,并偶然发现了温莎城堡的日志记录功能。 However, I'm kind of skeptical about whether I should use it or not. 但是,对于是否应该使用它,我有些怀疑。

public class MyClass
{
    public Castle.Core.Logging.ILogger Logger { get; set; }

    ...
}

Windsor's logging facility requires that you expose your logger as a property. 温莎的日志记录工具要求您将记录器公开为属性。 Is that really a good practice? 那真的是个好习惯吗? I feel like I'm almost breaking encapsulation because normally when I reuse a component, I don't care about it's logging mechanism and I don't normally want to see it exposed. 我觉得我几乎要破坏封装了,因为通常当我重用一个组件时,我不在乎它的日志记录机制,而且我通常不希望看到它的公开。

If I use a custom wrapper that uses a static class to create the log, I can keep it private. 如果我使用使用静态类的自定义包装器来创建日志,则可以将其设置为私有。 For example: 例如:

public class MyClass
{
    private static MyCustomWrapper.ILogger Logger = LogManager.GetLogger(typeof(MyClass));

    ...
}

I've searched the web for reasons why I should use the logging facility, but I'm only finding articles on how to use it, but not why I should use it. 我在网络上搜索了为什么我应该使用日志记录工具的原因,但是我只找到有关如何使用它的文章,而不是为什么我应该使用它。 I feel like I'm missing the point. 我觉得我错过了重点。 Having the logging component exposed is kind of scarying me away. 暴露日志组件有点吓我一跳。

Windsor's logging facility requires that you expose your logger as a property. 温莎的日志记录工具要求您将记录器公开为属性。

Not necessarily. 不必要。 You can also put your logger as a constructor (ie mandatory) dependency. 您还可以将记录器作为构造器(即强制性)依赖项。 The logger is usually declared as a property (ie optional dependency) because there might be no logger. 通常将记录器声明为属性(即可选依赖项),因为可能没有记录器。 That is, the component should be able to function without a logger. 也就是说,该组件应该能够在没有记录器的情况下运行。

If I use a custom wrapper that uses a static class to create the log 如果我使用使用静态类的自定义包装器来创建日志

That's a service locator, that code couples MyClass to LogManager , which is IMHO worse than what you were trying to get away from. 那是一个服务定位器,代码将MyClass耦合到LogManager ,恕我直言,这比您想摆脱的要差。

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

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