ILoggerFacade with IEventAggregator

Topics: Prism v4 - WPF 4
Jan 11, 2011 at 1:15 PM

Hi,

I want to use a logger that will send a special CompositeAggregationEvent through EventAggregator everytime the ILoggerFacade.Log is called.

But how can I resolve my Logger, that uses IEventAggregator, in the CreateLogger event in the Bootstrapper when the MEF Container has not been build yet and so I cant use ServiceLocator nor Container.GetExportedValue<>?

 

 

Developer
Jan 11, 2011 at 4:27 PM
Edited Jan 11, 2011 at 4:28 PM

Hi,

One possible approach for achieving that could be to make the CreateLogger method create the default implementation for ILoggerFacade, and then override the RegisterBootstrapperProvidedTypes method (which is the one that export the logger to the MEF container) to export your implementation of the logger. At the time the RegisterBootstrapperProvidedTypes method is called, the MEF container is available, so you should be able to make your logger obtain a reference to the Event Aggregator.

 

protected override void RegisterBootstrapperProvidedTypes()
        {
            this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger); // replace this.Logger for your implementation of ILoggerFacade
            this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
            this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
            this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
        }

 

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 12, 2011 at 7:17 AM

Hi,

thank you, this approach works.

To be honest, I have tried almost the same code before I posted my question here...

The only flaw is that all events from the initialization sequence of the application are beeing send to default PRISM Logger (the logger created in the CreateLogger method). And my logger kicks in after the "Initializing shell" event.

Is there any chance that my Logger (with EventAggregator support) could be used for the events from the Initialization sequence?

I guess not because I can't compose my Logger before the MEF container is available, but I am asking anyway...

 

Thank you

 

Developer
Jan 12, 2011 at 2:33 PM

Hi,

The approach you're mentioning is possible, but you should create a logger that uses the Event Aggregator only when it's available. To that purpose, you could use Optional Imports and Recomposition.

However, if you publish an event with the Event Aggregator before any modules are loaded, it's likely that there won't be a loosely coupled subscriber available, hence you might probably not benefit from the Event Aggregator capabilities in such case.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 13, 2011 at 11:11 AM
GuidoMaliandi wrote:

However, if you publish an event with the Event Aggregator before any modules are loaded, it's likely that there won't be a loosely coupled subscriber available, hence you might probably not benefit from the Event Aggregator capabilities in such case.


Thank you for the advice.

You are abosolutely right and such approach is useless when the modules aren't loaded...

I have decided to modify the source code of the PRISM and added a standart .NET event to the ILoggerFacade interface.

This event is then raised everytime the Log() method is called.