Using WPF and Prism/Unity with Entlib5 Logging

Topics: Prism v4 - WPF 4
Apr 26, 2011 at 4:53 PM

Hi folks,

I originally posted this question in the entlib forum to no avail. I was told I might have better luck here because it involves Prism/Unity MVVM:

I'm having a problem using the Ent5 logging block with WPF Designtime data. The applications work perfectly, but after I 'tacked on' the logging block, when I try to open the WPF form in the designer (the form uses design time data), the logging block can't be instanciated. The designer throws the exception "The type LogWriter cannot be constructed. You must configure the container to supply this value.". Fine, it's probably because the designer can't find the entlib config used by my main app (app.config). So I tried creating an app.config for the MVVM module I'm working on (copied the entlib config items to it from the main app.config) and still no joy. I know the problem is coming from my designtime class constructor as shown below (the ExtendedLogger class):

        public CornerPadDtViewModel() : base(new RegionManager(), new UnityContainer(), new ExtendedLogger())
            CornerPadVm = new UserInput
                                  SalesOrder = "XXXX1234567", blah, blah...

I'm not even sure how to mock it, because I'm not too sure why it's not finding my new 'local' app.config in the module I'm working on in the first place. The app runs great, logging is slick, it's just the design time data issue. (sigh)

Soooo... how in the world can I get my designtime class back if I'm using the logging block in the MVVM module and it uses constructor injection?

Thanks for any help here.

Apr 26, 2011 at 7:22 PM


Based on my understanding of your scenario, you need to register your logger class in the Unity containter. This is done in the bootstrapper´s ConfigureContainer method.

By registering the logger in the container you could try to resolve it in your view model class like this:

public CornerPadDtViewModel(ILoggerExtended logger)
     this.MyLogger = logger;

Take into account that CornerPadDtViewModel is not actually resolving it dependencies with Unity, since you are creating a new instance of the logger class. Also you don´t need to create a new instance of the RegionManager in the view model, since it is already registered in the container you run the application for the first time.

Additionally, you can read Chapter 3: Managing Dependencies Between Components where container topics are covered.

Also if you continue experiencing this issue, it could be helpful if you could provide us with a repro sample of your solution, so that we can help you to identify the cause of your problem.


Miguel Bronzovic


Apr 26, 2011 at 8:46 PM

Thanks for the reply. Perhaps I did'nt make my situation too clear. Allow me to clarify.

The class CornerPadDtViewModel is derived from my runtime class CornerPadViewModel which has this signature:

        public CornerPadViewModel(IRegionManager regionManager, IUnityContainer container, IExtendedLogger logger)
            _logger = logger;
            _container = container;


The logger class is registered in Unity Container, because it resolves perfectly at runtime.

The class CornerPadDtViewModel is derived from CornerPadViewModel for design time purposes only. This allows me to instanciate the view model class so the design time values can be bound to the controls. In order to do this, I must instanciate it in this manner:

        public CornerPadDtViewModel() : base(new RegionManager(), new UnityContainer(), new ExtendedLogger())
            CornerPadVm = new UserInput
                                  SalesOrder = "XXXX1234567",
                                  CustomerName = "012345678901234567890123456789",
                                  QtyPerBale = 9999,
                                  QtyCornerPadsToPrint = 4,

Paying specific attention to the base(new.. . Since Unity is not available to resolve any of the base class constructor parameters, I have to NEW them up. This works fine, until I get to new ExtendedLogger(). I can't use the syntax ILoggerExtended logger here because the IDE throws a warning of "Interface name is not valid at this point".

Unless there is another way of creating a design time class based on my run time class...

Thanks, if you need more code, I would be happy to provide it.


Apr 27, 2011 at 2:42 PM

Hi John,

Based on my understanding of your scenario, you could create a dummy Extended Logger, which must implement the ILoggerExtended interface, so that you can pass it as a parameter to the constructor of your design time ViewModel, thus avoiding the errors that arise from creating the default ExtendedLogger in design time.

Additionally, you might find the Blendability section in the UI Composition chapter of the Prism MSDN documentation useful, as it provides alternative strategies and information for design time support in Prism.

I hope you find this helpful.

Guido Leandro Maliandi

Apr 28, 2011 at 12:56 PM

Using the logger in the Designer mode is not a good practice. Looks like you use the constructor injection, you may consider property injection for the Models.

Before using Log, it should be null checked or if it is in designer mode, do not log anything..

You have less luck with Bootstapper to configure the extended log for design time, since Bootstrapper is called for the application but not for the design time.

My advice would be fix the code which uses log in the designer mode than adding design-time extensions..


Gopalakrishnan Subramani

Apr 28, 2011 at 2:00 PM

Thanks Guido,

Your suggestion worked perfectly. Why I didn't think of that before, I don't know. I added a simple dummy class as you suggested that inherits from IExtended Logger and does nothing and Presto!, I have my design time data back.

Thank you for 'waking me up'. It was too simple. :-)