private static IObservable <Unit> RegisterListener(this XafApplication application,
                                                           ReactiveTraceListener traceListener)
        {
            var register = application.Modules.WhenListChanged().SelectMany(_ => _.list.ToTraceSource().ToObservable(Scheduler.Immediate))
                           .Do(_ => {
                if (!_.traceSource.Listeners.Contains(traceListener))
                {
                    _.traceSource.Listeners.Add(traceListener);
                }
            }).ToUnit();
            var applyModel = application.ReactiveModulesModel().ReactiveLogger()
                             .Select(logger => logger)
                             .Select(model => model.GetActiveSources())
                             .Do(modules => {
                foreach (var module in modules)
                {
                    var tuples = application.Modules.Where(m => m.Name == module.Id()).ToTraceSource();
                    foreach (var tuple in tuples)
                    {
                        tuple.traceSource.Switch.Level = module.Level;
                        if (!tuple.traceSource.Listeners.Contains(traceListener))
                        {
                            tuple.traceSource.Listeners.Add(traceListener);
                        }
                    }
                }
            })
                             .ToUnit();

            return(register.Merge(applyModel));
        }
        internal static IObservable <Unit> Connect(this ReactiveLoggerModule reactiveLoggerModule)
        {
            var application = reactiveLoggerModule.Application;

            if (application == null)
            {
                return(Observable.Empty <Unit>());
            }

            var listener = new ReactiveTraceListener(application.Title);

            ListenerEvents = listener.EventTrace.Publish().RefCount();


            return(application.BufferUntilCompatibilityChecked(ListenerEvents.Select(_ => _))
                   .SaveEvent(application)
                   .ToUnit()
                   .Merge(ListenerEvents.RefreshViewDataSource(application))
                   .Merge(application.RegisterListener(listener), Scheduler.Immediate));
        }