NLog和Prism的正确实现

发布于 2024-09-01 16:40:33 字数 290 浏览 6 评论 0原文

在我的 Prism / CAL WPF 应用程序中实现 NLog 的最佳方法是什么?这可能是一个业余问题,我对整个 Prism 框架有点陌生:)

我考虑将对 NLog dll 的引用放在基础设施模块中,并制作一个包装单例类,例如 MyLogger。我的想法是能够在所有内容都引用的中心位置的某个地方引用 1 个记录器实现,而我在 Prism 中唯一知道的是您的基础设施模块。

另一种明显的方法是向每个模块添加对 NLog 的引用,但我认为这会破坏解耦的目的以及所有这些。

任何想法都会是最有帮助的

问候

What would be the best way to implement NLog in my Prism / CAL WPF application. This might be an amateur question, I am a bit new to the whole Prism framework :)

I thought about putting the reference to the NLog dll in the Infrastructure module and make a wrapper singleton class e.g. MyLogger. My thinking was to be able to have the reference to 1 logger implementation somewhere in a central place that everything has reference to, and the only thing that I know of in Prism would be your Infrastructure module.

The obvious other way is to add a reference to NLog to each module but I think that would defeat the purpose of decoupling and all of that.

Any ideas would be most helpful

Regards

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

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

发布评论

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

评论(1

疯到世界奔溃 2024-09-08 16:40:34

我会推荐与您的第一个想法类似的东西,尽管它利用了 Prism 中已有的界面。

虽然我不确定 NLog 中可用的确切方法签名,但您可能需要考虑使用 Prism 的 ILoggerFacade 接口,通常在 Bootstrapper 中定义(请参阅 StockTraderRI 应用程序,了解如何设置的示例)。通常,这充当 Microsoft 的复合日志记录界面的传递,但是您没有理由不能使用它来连接您自己的记录器。

考虑这种方法的几个原因:

  1. 它使用 Prism 框架中已经存在的 ILoggerFacade 接口,其他开发人员会熟悉该接口
  2. 如果您稍后决定使用不同的日志记录框架,则只需替换 ILoggerFacade 实现背后的对象

另一种方法是按照您的建议进行操作:在基础结构 DLL 中创建一个定义 NLog 服务的接口(或公开现有的 NLog 接口),并在引导程序中注册该服务的实现。然后,您可以使用依赖项注入容器来获取对模块中记录器服务的引用。但请注意,这实际上只是重现了 ILoggerFacade 接口已经为您提供的内容。

I would recommend something similar to your first idea, although it leverages an already existing interface in Prism.

While I'm not sure the exact method signatures available to you in NLog, you may want to consider using Prism's ILoggerFacade interface, which is typically defined in your Bootstrapper (see the StockTraderRI application for an example of how this is set up). Typically, this acts as a pass through to Microsoft's Composite Logging interface, but there's no reason why you can't use this to hook into your own logger.

A few reasons to consider this approach:

  1. It uses the already existing ILoggerFacade interface in the Prism framework, which other developers will be familiar with
  2. If you later decide to go to a different logging framework, you just have to replace the object behind the ILoggerFacade implementation

The other approach would be to do as you suggest: create an interface that defines a service to NLog (or expose an existing NLog interface) in your infrastructure DLL and register the implementation of that service in your bootstrapper. You can then you your dependency injection container to get a reference to the logger service in your modules. Note, however, that this really just reproduces what the ILoggerFacade interface already gives you.

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