コード例 #1
0
        public void LogDoesNotAddEmptyScopeToExtendedProperties()
        {
            LogEntry logEntry  = null;
            var      exception = new Exception("Some random exception");
            var      eventId   = new EventId(15, "SomeEvent");
            var      state     = new Dictionary <string, string>();

            var loggerMock = new Mock <ILogger>();

            loggerMock.Setup(lm => lm.Level).Returns(LogLevel.Debug);
            loggerMock
            .Setup(lm => lm.Log(It.IsAny <LogEntry>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Callback <LogEntry, string, string, int>((le, s1, s2, i) => logEntry = le);

            var rlLogger = new RockLibLogger(loggerMock.Object, "SomeCategory");

            using (rlLogger.BeginScope("a"))
                using (rlLogger.BeginScope("b"))
                    using (rlLogger.BeginScope("c"))
                    {
                    }

            rlLogger.Log(MSE.LogLevel.Debug, eventId, state, exception, (dictionary, ex) => "Simple message");

            logEntry.ExtendedProperties.Should().NotContainKey("Microsoft.Extensions.Logging.Scope");
        }
コード例 #2
0
        public void BeginScopeAddsObjectToTheScopeStack()
        {
            var loggerMock = new Mock <ILogger>();

            var rlLogger = new RockLibLogger(loggerMock.Object, null);

            rlLogger.GetScope().Should().BeEmpty();

            rlLogger.BeginScope("a");
            rlLogger.GetScope().Should().Equal(new object[] { "a" });

            rlLogger.BeginScope("b");
            rlLogger.GetScope().Should().Equal(new object[] { "b", "a" });

            rlLogger.BeginScope("c");
            rlLogger.GetScope().Should().Equal(new object[] { "c", "b", "a" });
        }
コード例 #3
0
        public void BeginScopeMethodHappyPath2()
        {
            var logger = new MockLogger().Object;

            var rockLibLogger = new RockLibLogger(logger, "MyCategory", null);

            var token = rockLibLogger.BeginScope("Hello, world!");

            token.Should().BeNull();
        }
コード例 #4
0
        public void DisposingTheObjectReturnedFromBeginScopePopsTheScopeStack()
        {
            var loggerMock = new Mock <ILogger>();

            var rlLogger = new RockLibLogger(loggerMock.Object, null);

            using (rlLogger.BeginScope("a"))
            {
                using (rlLogger.BeginScope("b"))
                {
                    using (rlLogger.BeginScope("c"))
                    {
                        rlLogger.GetScope().Should().Equal(new object[] { "c", "b", "a" });
                    }
                    rlLogger.GetScope().Should().Equal(new object[] { "b", "a" });
                }
                rlLogger.GetScope().Should().Equal(new object[] { "a" });
            }

            rlLogger.GetScope().Should().BeEmpty();
        }
コード例 #5
0
        public void BeginScopeMethodHappyPath1()
        {
            var expectedToken     = new Mock <IDisposable>().Object;
            var mockScopeProvider = new Mock <IExternalScopeProvider>();

            mockScopeProvider.Setup(m => m.Push(It.IsAny <object>())).Returns(expectedToken);
            var logger = new MockLogger().Object;

            var rockLibLogger = new RockLibLogger(logger, "MyCategory", mockScopeProvider.Object);

            var token = rockLibLogger.BeginScope("Hello, world!");

            mockScopeProvider.Verify(m => m.Push("Hello, world!"), Times.Once());
            token.Should().BeSameAs(expectedToken);
        }