Example #1
0
        public void TestPhaseScope()
        {
            var listener = new TestLoggerListener {
                LogLevelThreshold = LogLevel.Diagnostic
            };
            var      logLevel = Logger.LogLevelThreshold;
            ILogItem item;

            try
            {
                Logger.LogLevelThreshold = LogLevel.Diagnostic;
                Logger.RegisterListener(listener);
                Action <bool> callback;

                Logger.LogInfo("test no phase scope");
                Assert.Null(listener.TakeAndRemove().Phase);

                using (new LoggerPhaseScope("A"))
                {
                    Logger.LogInfo("test in phase scope A");
                    Assert.Equal("A", listener.TakeAndRemove().Phase);

                    using (new LoggerPhaseScope("B"))
                    {
                        Logger.LogInfo("test in phase scope B");
                        Assert.Equal("A.B", listener.TakeAndRemove().Phase);

                        var captured = LoggerPhaseScope.Capture();
                        Assert.NotNull(captured);
                        callback = shouldLogPerformance =>
                        {
                            using (LoggerPhaseScope.Restore(captured, shouldLogPerformance))
                            {
                                Logger.LogInfo("test in captured phase scope B");
                            }
                        };
                    } // exit scope B.

                    using (new LoggerPhaseScope("C", true))
                    {
                        Logger.LogInfo("test in phase scope C");
                        Assert.Equal("A.C", listener.TakeAndRemove().Phase);

                        // run callback in scope C.
                        callback(false);
                        Assert.Equal("A.B", listener.TakeAndRemove().Phase);
                    } // exit scope C.

                    item = listener.TakeAndRemove();
                    Assert.Equal("A.C", item.Phase);
                    Assert.Equal(LogLevel.Diagnostic, item.LogLevel);
                } // exit scope A.

                Logger.LogInfo("test no phase scope");
                Assert.Null(listener.TakeAndRemove().Phase);

                // run callback in no scope.
                callback(true);
                Assert.Equal("A.B", listener.TakeAndRemove().Phase);
                item = listener.TakeAndRemove();
                Assert.Equal("A.B", item.Phase);
                Assert.Equal(LogLevel.Diagnostic, item.LogLevel);

                Logger.LogInfo("test no phase scope again");
                Assert.Null(listener.TakeAndRemove().Phase);
            }
            finally
            {
                Logger.UnregisterListener(listener);
                Logger.LogLevelThreshold = logLevel;
            }
        }
Example #2
0
        public void TestPhaseScope()
        {
            var      listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(null, LogLevel.Diagnostic);
            var      logLevel = Logger.LogLevelThreshold;
            ILogItem item;

            AmbientContext.InitializeAmbientContext("id");
            try
            {
                Logger.LogLevelThreshold = LogLevel.Diagnostic;
                Logger.RegisterListener(listener);
                Action <bool, int> callback;

                Logger.LogInfo("test no phase scope");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);
                Assert.Equal("id", AmbientContext.CurrentContext.Id);
                Assert.Equal("id.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                using (new LoggerPhaseScope("A"))
                {
                    Logger.LogInfo("test in phase scope A");
                    Assert.Equal("A", TakeFirstLogItemAndRemove(listener.Items).Phase);
                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);
                    Assert.Equal("id.3.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                    using (new LoggerPhaseScope("B"))
                    {
                        Logger.LogInfo("test in phase scope B");
                        Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);

                        Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                        Assert.Equal("id.3.3.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());

                        var captured = LoggerPhaseScope.Capture();
                        Assert.NotNull(captured);
                        callback = (shouldLogPerformance, round) =>
                        {
                            using (shouldLogPerformance ?
                                   LoggerPhaseScope.Restore(captured, LogLevel.Diagnostic) :
                                   LoggerPhaseScope.Restore(captured))
                            {
                                Logger.LogInfo("test in captured phase scope B");
                                if (round == 1)
                                {
                                    Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                                    Assert.Equal("id.3.3.4", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                                }

                                if (round == 2)
                                {
                                    Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                                    Assert.Equal("id.3.3.6", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                                }
                            }
                        };
                    } // exit scope B.

                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);

                    using (new LoggerPhaseScope("C", LogLevel.Diagnostic))
                    {
                        Logger.LogInfo("test in phase scope C");

                        Assert.Equal("A.C", TakeFirstLogItemAndRemove(listener.Items).Phase);

                        Assert.Equal("id.3.4", AmbientContext.CurrentContext.Id);

                        // run callback in scope C.
                        callback(false, 1);

                        Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);
                        Assert.Equal("id.3.4", AmbientContext.CurrentContext.Id);
                    } // exit scope C.

                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);

                    item = TakeFirstLogItemAndRemove(listener.Items);
                    Assert.Equal("A.C", item.Phase);
                    Assert.Equal(LogLevel.Diagnostic, item.LogLevel);
                } // exit scope A.

                Assert.Equal("id", AmbientContext.CurrentContext.Id);

                Logger.LogInfo("test no phase scope");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);

                // run callback in no scope.
                callback(true, 2);
                Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);
                item = TakeFirstLogItemAndRemove(listener.Items);
                Assert.Equal("A.B", item.Phase);
                Assert.Equal(LogLevel.Diagnostic, item.LogLevel);

                Logger.LogInfo("test no phase scope again");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);
            }
            finally
            {
                Logger.UnregisterListener(listener);
                Logger.LogLevelThreshold = logLevel;
                AmbientContext.CurrentContext.Dispose();
            }
        }