Prism 4, Unity, and Enterprise Library Logging and Exception Handling block

Topics: Exception Handling Application Block, Logging Application Block
Nov 2, 2012 at 2:30 PM

I'm building a WPF application. I'm using Prism 4, and Unity. I want to add two Enterprise Library 5 blocks to the application, Logging and Exception Handling.

For logging, I have a singleton LoggerFacadeCustom.cs in my Infrastructure layer that supports the ILoggerFacade interface. 

I've created it in my bootstrapper, and it is generating log files. It "news" up a unity container in its constructor.

How do you integrate Exception Handling block with the logger in this configuration? How do you start Unity with the logger and exception handler together? Is a singleton really required?

   public class LoggerFacadeCustom : ILoggerFacade {

      private static readonly LoggerFacadeCustom _instance = new LoggerFacadeCustom();
      public static LoggerFacadeCustom Instance { get { return _instance; } }

      private LoggerFacadeCustom() {
         var container = new UnityContainer();
         container.AddNewExtension<EnterpriseLibraryCoreExtension>();

         _logWriter = container.Resolve<LogWriter>();
      }

      private readonly LogWriter _logWriter;


      public void Write(string message) { Write(message, null); }

      public void Write(string message, string category, int priority) {
         _logWriter.Write(message, category, priority);
      }

      public void Write(string message, Dictionary<string, object> properties) {
         _logWriter.Write(message, LiteralString.LogCategoryProcess, properties);
      }


      #region ILoggerFacade Members

      public void Log(string message, Category category, Priority priority) {
         throw new NotImplementedException();
      }

      #endregion
   }
My bootstrapper looks like this:
      public class Bootstrapper : UnityBootstrapper {

         protected override ILoggerFacade CreateLogger() {
            return LoggerFacadeCustom.Instance;
         }

         protected override DependencyObject CreateShell() {
            return Container.Resolve<Shell>();
         }

         protected override void InitializeShell() {
            base.InitializeShell();

            App.Current.MainWindow = (Window)Shell;
            App.Current.MainWindow.Show();

         //Other shell stuff...
 
         }

         protected override IModuleCatalog CreateModuleCatalog() {

            var catalog = new ModuleCatalog();

            //These primary modules must register their own services as if they were acting independantly
            catalog.AddModule(typeof(XmlCommentMergeModule));

            //These support modules require at least one primary module above to be added first
            catalog.AddModule(typeof(ToolboxHeaderModule));
            catalog.AddModule(typeof(ToolboxFooterModule));
            catalog.AddModule(typeof(ToolboxStartModule));
            return catalog;
         }
      }
Nov 4, 2012 at 11:29 PM

As a caveat, I'm not a Prism expert (by any means).

I don't think you need a Singleton if you just maintain a reference to your ILoggerFacade in the Bootstrapper and return that instance via CreateLogger().

What exactly do you mean by "integrate Exception Handling block with the logger".  Do you mean have the Exception Handling block use the ILoggerFacade?  If you want to do that I think you would need a custom Exception Handler since Enterprise Library itself does not know about ILoggerFacade.

If you want to share the Logging and Exception Handling configuration you could look at resolving the LogWriter and ExceptionManager using the common  EnterpriseLibraryContainer Service Locator.  Internally it will share the same container so everything should be wired properly.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 5, 2012 at 2:13 PM

I’m new to Enterprise Library myself, so I guess we should both proceed cautiously.

I’ve worked out how to insert the Enterprise Logger into Prism logging. You override the CreateLogger method, like so:

protected override ILoggerFacade CreateLogger() { return LoggerFacadeCustom.Instance; }

So that’s half the battle. I stayed with the singleton because I also needed to add additional logging methods that take advantage Enterprise Library rich logging process. I’m also not sure if the Prism, Unity container is the same as the Enterprise Library container and I would like to reach the same instance of LoggerFacadeCustom in other locations in my application.

Now I’m left with the other half of my question. How do I apply (integrate) Enterprise Library Exception Handling block to my Prism, Unity application?

Thoughts?

Jim

Nov 5, 2012 at 4:52 PM

Normally I would say just register everything in one container and you should be good to go.  The issue as I see it is that the Bootstrapper process (as described in Initializing Prism Applications) creates the logger as the first step before even the container is created.  

One thing you could is create a singleton class that creates the container.  This singleton class could then be used by the Logger and also accessed by the CreateContainer method of Bootstrapper.  Then the Logger and ExceptionManager would be registered in the same container.  The Prism container should then be able to resolve instances of ExceptionManager to inject into your classes.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com