温莎城堡伐木设施

发布于 2024-10-03 01:53:32 字数 632 浏览 4 评论 0原文

我正在尝试删除一些日志记录依赖项,并偶然发现了温莎城堡的日志记录设施。然而,我对是否应该使用它有点怀疑。

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

    ...
}

温莎的日志记录工具要求您将记录器作为属性公开。这真的是一个好的做法吗?我觉得我几乎打破了封装,因为通常当我重用一个组件时,我不关心它的日志机制,而且我通常不想看到它被暴露。

如果我使用使用静态类创建日志的自定义包装器,我可以将其保持私有。例如:

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

    ...
}

我在网络上搜索了为什么应该使用日志记录工具的原因,但我只找到了关于如何使用它的文章,而不是如何使用它的文章。 >为什么我应该使用它。我觉得我没有抓住要点。暴露日志组件有点让我害怕。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-10-10 01:53:32

Windsor 的日志记录工具要求您将记录器公开为属性。

未必。您还可以将记录器作为构造函数(即强制)依赖项。记录器通常被声明为属性(即可选依赖项),因为可能没有记录器。也就是说,该组件应该能够在没有记录器的情况下运行。

如果我使用使用静态类的自定义包装器来创建日志

,这是一个服务定位器,该代码将 MyClass 耦合到 LogManager,恕我直言,这比您尝试的更糟糕远离。

Windsor's logging facility requires that you expose your logger as a property.

Not necessarily. You can also put your logger as a constructor (i.e. mandatory) dependency. The logger is usually declared as a property (i.e 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文