Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top