CompositeWPF - Hijacking the Logger

For reasons that will become readily apparent I am replacing the default logger with TraceTool - the CompositeWPF documentation pretty much tells us to do it as follows in the bootstrapper:

        private TraceToolLogger logger = new TraceToolLogger();
        /// <summary>
        /// Override loggerFacade to use the TraceToolLogger
        /// </summary>
        protected override ILoggerFacade LoggerFacade
        {
            get
            {
                return logger;
            }
        }

This is easy!  The only problem is we have only one Log method with the ILoggerFacade and I want to take advantage of all the power the TraceTool offers while providing backwards compatibility with all existing log calls; the ILoggerFacade interface follows:

    public interface ILoggerFacade
    {
        void Log(string message, Category category, Priority priority);
    }

I'll let Polymorphism help me here - note above I am using TraceToolLogger.  The TraceToolLogger code below reveals that it implements my new ILogger interface! 

namespace CompositeWPF.Library.Core
{
    public class TraceToolLogger : ILogger
    {
        public void Log(string message, Category category, Priority priority)
        {
            string leftMsg = string.Format("{0}[{1}]", category, priority);
            switch (category)
            {
                case Category.Debug:
                    TTrace.Debug.Send(leftMsg, message);
                    break;
                case Category.Exception:
                    TTrace.Error.Send(leftMsg, message);
                    break;
                case Category.Info:
                    TTrace.Debug.Send(leftMsg, message);
                    break;
                case Category.Warn:
                    TTrace.Warning.Send(leftMsg, message);
                    break;
            }
        }

        public void Log(string leftMsg, object objToSend, TraceDisplayFlags flag)
        {
            TTrace.Debug.SendObject(leftMsg, objToSend, flag);
        }
        /// <summary>
        /// Shows object fields
        /// </summary>
        /// <param name="leftMsg"></param>
        /// <param name="objToSend"></param>
        public void Log(string leftMsg, object objToSend)
        {
            Log(leftMsg, objToSend, TraceDisplayFlags.ShowFields);
        }
    }

By implementing the ILoggerFacade interface on my ILogger interface I'll be allowed to assign the TraceToolLogger instance to the LoggerFacade; my TraceToolLogger will be backwards compatible while making ILogger available for dependency injection.

namespace CompositeWPF.Library.Interface.Interfaces
{
    public interface ILogger : ILoggerFacade
    {
        void Log(string leftMsg, object objToSend, TraceDisplayFlags flag);
        void Log(string leftMsg, object objToSend);
    }
}

Click HERE for flash demo

The above output resulted from the below code.  I am using the TraceTool SendObject() method to display the class contents - in this case of Test.   This will permit me to debug trace objects and their contents which will become invaluable when I start plugging in my SQL Server stored procedure calls - I'll be able to output my parameter object so that I can see at a glance what is being sent to the backend - AND - what is being returned.

Source code available HERE - Change Set: 22520

 


Tags: , ,
Categories: CompositeWPF


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know