public void BuilderCreatesFactoryWithCustomFilters()
        {
            var factory = new LogFactoryBuilder()
                          .UseTraceSource()
                          .SetMinimumLogLevel(LogLevel.Information)
                          .AddFilter("Decos.Diagnostics", LogLevel.Debug)
                          .Build();

            Assert.IsTrue(factory.Create("Decos.Diagnostics").IsEnabled(LogLevel.Debug));
            Assert.IsFalse(factory.Create("Decos").IsEnabled(LogLevel.Debug));
        }
        public async Task FactoryShutdownCanBeCancelled()
        {
            var listener = new DelayAsyncTraceListener(100);
            var factory  = new LogFactoryBuilder()
                           .UseTraceSource()
                           .AddTraceListener(listener)
                           .Build();

            var log = factory.Create("Test");

            for (int i = 0; i < 10; i++)
            {
                log.Info(i);
            }

            var cancellationTokenSource = new CancellationTokenSource(250);

            try
            {
                await factory.ShutdownAsync(cancellationTokenSource.Token);
            }
            catch (OperationCanceledException) { }

            Assert.AreNotEqual(0, listener.QueueCount);
        }
        public void BuilderCreatesFactoryWithDefaultOptions()
        {
            var factory = new LogFactoryBuilder()
                          .UseTraceSource()
                          .Build();

            var log = factory.Create("Test");

            Assert.IsTrue(log.IsEnabled(LogLevel.Information));
        }
        public void BuilderCreatesFactoryWithCustomLogLevel()
        {
            var factory = new LogFactoryBuilder()
                          .UseTraceSource()
                          .SetMinimumLogLevel(LogLevel.Debug)
                          .Build();

            var log = factory.Create("Test");

            Assert.IsTrue(log.IsEnabled(LogLevel.Debug));
        }
        public void BuilderAddsTraceListenersUponBuild()
        {
            var traceListener = new NullTraceListener();
            var factory       = new LogFactoryBuilder()
                                .UseTraceSource()
                                .AddTraceListener(traceListener)
                                .Build();

            var log = (TraceSourceLog)factory.Create("Test");

            CollectionAssert.Contains(log.TraceSource.Listeners, traceListener);
        }
        public async Task FactoryOnlyHooksOncePerAsyncListener()
        {
            var listener = new DelayAsyncTraceListener(5);
            var factory  = new LogFactoryBuilder()
                           .UseTraceSource()
                           .AddTraceListener(listener)
                           .Build();

            for (int i = 0; i < 100; i++)
            {
                var log = factory.Create($"Logger{i}");
                log.Info($"{i}");
            }

            Assert.AreEqual(1, ((TraceSourceLogFactory)factory)._shutdownTasks.Count);
        }
        public void TraceListenerWithHigherMinimumLevelDoesNotGetCalled()
        {
            var traceListener = new DelayAsyncTraceListener(-1);
            var factory       = new LogFactoryBuilder()
                                .UseTraceSource()
                                .AddTraceListener(traceListener, LogLevel.Warning)
                                .SetMinimumLogLevel(LogLevel.Information)
                                .Build();

            var log = (TraceSourceLog)factory.Create("Test");

            Assert.IsTrue(log.IsEnabled(LogLevel.Information));

            log.Info("Test");
            Assert.IsFalse(traceListener.TraceCalled);
        }
        public void BuilderRunsAsyncListeners()
        {
            var listener = new DelayAsyncTraceListener(-1);
            var factory  = new LogFactoryBuilder()
                           .UseTraceSource()
                           .AddTraceListener(listener)
                           .Build();

            var log = factory.Create("Test");

            for (int i = 0; i < 100; i++)
            {
                log.Info(i);
            }

            Assert.IsTrue(listener.ProcessQueueAsyncCalled);
        }
        public async Task FactoryAllowsForGracefulShutdown()
        {
            var listener = new DelayAsyncTraceListener(100);
            var factory  = new LogFactoryBuilder()
                           .UseTraceSource()
                           .AddTraceListener(listener)
                           .Build();

            var log = factory.Create("Test");

            for (int i = 0; i < 10; i++)
            {
                log.Info(i);
            }

            await factory.ShutdownAsync();

            Assert.AreEqual(0, listener.QueueCount);
        }