温莎城堡伐木设施
我正在尝试删除一些日志记录依赖项,并偶然发现了温莎城堡的日志记录设施。然而,我对是否应该使用它有点怀疑。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未必。您还可以将记录器作为构造函数(即强制)依赖项。记录器通常被声明为属性(即可选依赖项),因为可能没有记录器。也就是说,该组件应该能够在没有记录器的情况下运行。
,这是一个服务定位器,该代码将
MyClass
耦合到LogManager
,恕我直言,这比您尝试的更糟糕远离。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.
That's a service locator, that code couples
MyClass
toLogManager
, which is IMHO worse than what you were trying to get away from.