//TODO That is not ready to be used with some IoC container like Castle or Unity
        public AsyncMessagePersister(IMessageWriterFactory factory, IMessageFactory messageFactory, IDateFactory dateFactory)
        {
            this.messageWriterFactory = factory;
            this.dateFactory          = dateFactory;
            this.messageFactory       = messageFactory;
            this.writer = CreateMessageWriter(factory);

            if (this.writer != null)
            {
                this.runThread = new Thread(this.Processing);
                this.runThread.Start();
            }
            else
            {
                //TODO instead of Console.Writeline it could be some other persister like log4net log
                Console.WriteLine("Cannot initialize Async Log!");
            }
        }
        private static IMessageWriter ConfigureStubsAndMocksForAsyncLog(out IMessagePersister persister,
                                                                        string testString,
                                                                        DateTime logTimeStamp,
                                                                        IDateFactory dateFactory,
                                                                        out IMessageWriterFactory mockMessageWriterFactory,
                                                                        int expecteNumberOfTimesCreateToCall)
        {
            mockMessageWriterFactory = MockRepository.GenerateMock <IMessageWriterFactory>();
            IMessageWriter  mockWriter         = MockRepository.GenerateMock <IMessageWriter>();
            IMessageFactory stubMessageFactory = MockRepository.GenerateStub <IMessageFactory>();
            IMessage        stubMessage        = MockRepository.GenerateStub <IMessage>();

            stubMessage.Stub(x => x.FormatMessage()).Return(testString);
            stubMessage.Timestamp = logTimeStamp;
            stubMessageFactory.Stub(x => x.GetMessage(Arg <string> .Is.Anything, Arg <DateTime> .Is.Anything))
            .Return(stubMessage);
            mockMessageWriterFactory.Expect(x => x.Create()).Return(mockWriter).Repeat.Times(expecteNumberOfTimesCreateToCall);
            persister = new AsyncMessagePersister(mockMessageWriterFactory, stubMessageFactory, dateFactory);
            return(mockWriter);
        }
        private IMessageWriter CreateMessageWriter(IMessageWriterFactory factory)
        {
            IMessageWriter writer = null;

            try
            {
                //TODO Currently this implementation is limited by one writer for the log e.g. FileMessageWriter or ConsoleWriter,etc
                //TODO It is possible to introduce concept of appenders instead of writers. E.g. to define more than one "appender"
                //TODO in application config, initialize collection of appenders and than call IAppedner.Persist() for every appender in collection
                writer = factory.Create();
            }
            catch (Exception ex)
            {
                //Catch exception per this requirment: If an error occur the calling application must not be put down.
                // It is more important that the application continues to  run than lines not being written to log
                Console.Write("Error creating MessagePersister: {0}", ex.Message);
                //throw new ApplicationException(String.Format("Error creating MessagePersister: {0}", ex.Message));
            }
            return(writer);
        }