Example #1
0
        public void LoggerPushScopePropertiesCovarianceTest(bool convertDictionary)
        {
            // Arrange
            ScopeContext.Clear();
            var expectedString = "World";
            var expectedId     = 42;
            IReadOnlyCollection <KeyValuePair <string, IConvertible> > expectedProperties = new[] { new KeyValuePair <string, IConvertible>("Hello", expectedString), new KeyValuePair <string, IConvertible>("RequestId", expectedId) };

            if (convertDictionary)
            {
                expectedProperties = expectedProperties.ToDictionary(i => i.Key, i => i.Value);
            }
            Dictionary <string, object> allProperties = null;
            object stringValueLookup = null;
            var    logger            = new LogFactory().GetCurrentClassLogger();

            // Act
            using (logger.PushScopeProperty("Hello", "People"))
            {
                using (logger.PushScopeProperties(expectedProperties))
                {
                    allProperties = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
                }
                ScopeContext.TryGetProperty("Hello", out stringValueLookup);
            }

            // Assert
            Assert.Equal(2, allProperties.Count);
            Assert.Equal(expectedString, allProperties["Hello"]);
            Assert.Equal(expectedId, allProperties["RequestId"]);
            Assert.Equal(expectedProperties.Select(p => new KeyValuePair <string, object>(p.Key, p.Value)), allProperties);
            Assert.Equal("People", stringValueLookup);
        }
Example #2
0
        public void LoggerPushPropertyTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedValue = "World";
            Dictionary <string, object> allProperties = null;
            var    success = false;
            object value;
            var    logger = new LogFactory().GetCurrentClassLogger();

            // Act
            using (logger.PushScopeProperty("HELLO", expectedValue))
            {
                success       = ScopeContext.TryGetProperty("hello", out value);
                allProperties = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
            }
            var failed = ScopeContext.TryGetProperty("hello", out var _);

            // Assert
            Assert.True(success);
            Assert.Equal(expectedValue, value);
            Assert.Single(allProperties);
            Assert.Equal(expectedValue, allProperties["HELLO"]);
            Assert.False(failed);
        }
Example #3
0
        public void LoggerPushScopePropertiesOverwriteTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedString     = "World";
            var expectedGuid       = System.Guid.NewGuid();
            var expectedProperties = new[] { new KeyValuePair <string, object>("Hello", expectedString), new KeyValuePair <string, object>("RequestId", expectedGuid) };
            Dictionary <string, object> allProperties = null;
            object stringValueLookup = null;
            var    logger            = new LogFactory().GetCurrentClassLogger();

            // Act
            using (logger.PushScopeProperty("Hello", "People"))
            {
                using (logger.PushScopeProperties(expectedProperties))
                {
                    allProperties = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
                }
                ScopeContext.TryGetProperty("Hello", out stringValueLookup);
            }

            // Assert
            Assert.Equal(2, allProperties.Count);
            Assert.Equal(expectedString, allProperties["Hello"]);
            Assert.Equal(expectedGuid, allProperties["RequestId"]);
            Assert.Equal(expectedProperties.Select(p => new KeyValuePair <string, object>(p.Key, p.Value)), allProperties);
            Assert.Equal("People", stringValueLookup);
        }
Example #4
0
        public void LoggerPushScopePropertiesTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedString     = "World";
            var expectedGuid       = System.Guid.NewGuid();
            var expectedProperties = new[] { new KeyValuePair <string, object>("Hello", expectedString), new KeyValuePair <string, object>("RequestId", expectedGuid) };
            IEnumerable <KeyValuePair <string, object> > allPropertiesState = null;
            Dictionary <string, object> allProperties = null;
            var    logger            = new LogFactory().GetCurrentClassLogger();
            object stringValueLookup = null;

            // Act
            using (logger.PushScopeProperties(expectedProperties))
            {
                allPropertiesState = ScopeContext.GetAllProperties();
                allProperties      = allPropertiesState.ToDictionary(x => x.Key, x => x.Value);
            }
            ScopeContext.TryGetProperty("Hello", out stringValueLookup);

            // Assert
#if !NET35 && !NET40 && !NET45
            Assert.Same(expectedProperties, allPropertiesState);
#endif
            Assert.Equal(2, allProperties.Count);
            Assert.Equal(expectedString, allProperties["Hello"]);
            Assert.Equal(expectedGuid, allProperties["RequestId"]);
            Assert.Equal(expectedProperties.Select(p => new KeyValuePair <string, object>(p.Key, p.Value)), allProperties);
            Assert.Null(stringValueLookup);
        }
Example #5
0
        public void PushNestedStatePropertiesTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedString      = "World";
            var expectedGuid        = System.Guid.NewGuid();
            var expectedProperties  = new[] { new KeyValuePair <string, object>("Hello", expectedString), new KeyValuePair <string, object>("RequestId", expectedGuid) };
            var expectedNestedState = "First Push";
            Dictionary <string, object> allProperties = null;

            object[] allNestedStates   = null;
            object   stringValueLookup = null;

            // Act
            using (ScopeContext.PushProperty("Hello", "People"))
            {
                using (ScopeContext.PushNestedStateProperties(expectedNestedState, expectedProperties))
                {
                    allNestedStates = ScopeContext.GetAllNestedStates();
                    allProperties   = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
                }
                ScopeContext.TryGetProperty("Hello", out stringValueLookup);
            }

            // Assert
            Assert.Equal(2, allProperties.Count);
            Assert.Equal(expectedString, allProperties["Hello"]);
            Assert.Equal(expectedGuid, allProperties["RequestId"]);
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
            Assert.Equal("People", stringValueLookup);
        }
Example #6
0
        public void PushPropertyNestedTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedString = "World";
            var expectedGuid   = System.Guid.NewGuid();
            Dictionary <string, object> allProperties = null;
            object stringValueLookup1 = null;
            object stringValueLookup2 = null;
            bool   stringValueLookup3 = false;
            object guidValueLookup1   = null;
            bool   guidValueLookup2   = false;
            bool   guidValueLookup3   = false;

            // Act
            using (ScopeContext.PushProperty("Hello", expectedString))
            {
                using (ScopeContext.PushProperty("RequestId", expectedGuid))
                {
                    ScopeContext.TryGetProperty("Hello", out stringValueLookup1);
                    ScopeContext.TryGetProperty("RequestId", out guidValueLookup1);
                    allProperties = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
                }

                ScopeContext.TryGetProperty("Hello", out stringValueLookup2);
                guidValueLookup2 = ScopeContext.TryGetProperty("RequestId", out var _);
            }
            guidValueLookup3   = ScopeContext.TryGetProperty("RequestId", out var _);
            stringValueLookup3 = ScopeContext.TryGetProperty("Hello", out var _);

            // Assert
            Assert.Equal(2, allProperties.Count);
            Assert.Equal(expectedString, allProperties["Hello"]);
            Assert.Equal(expectedGuid, allProperties["RequestId"]);
            Assert.Equal(expectedString, stringValueLookup1);
            Assert.Equal(expectedString, stringValueLookup2);
            Assert.Equal(expectedGuid, guidValueLookup1);
            Assert.False(guidValueLookup2);
            Assert.False(guidValueLookup3);
            Assert.False(guidValueLookup3);
            Assert.False(stringValueLookup3);
        }
Example #7
0
        public void PushPropertyCaseInsensitiveTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedValue = "World";
            Dictionary <string, object> allProperties = null;
            var    success = false;
            object value;

            // Act
            using (ScopeContext.PushProperty("HELLO", expectedValue))
            {
                success       = ScopeContext.TryGetProperty("hello", out value);
                allProperties = ScopeContext.GetAllProperties().ToDictionary(x => x.Key, x => x.Value);
            }
            var failed = ScopeContext.TryGetProperty("hello", out var _);

            // Assert
            Assert.True(success);
            Assert.Equal(expectedValue, value);
            Assert.Single(allProperties);
            Assert.Equal(expectedValue, allProperties["HELLO"]);
            Assert.False(failed);
        }
            protected override void Write(LogEventInfo logEvent)
            {
                if (!SkipAssert)
                {
                    Assert.True(logEvent.HasStackTrace);

                    var scopeProperties = ScopeContext.GetAllProperties();  // See that async-timer cannot extract anything from scope-context
                    Assert.Empty(scopeProperties);

                    var scopeNested = ScopeContext.GetAllNestedStates();    // See that async-timer cannot extract anything from scope-context
                    Assert.Empty(scopeNested);
                }

                LastCombinedProperties = base.GetAllProperties(logEvent);

                var nestedStates = base.GetScopeContextNestedStates(logEvent);

                if (nestedStates.Count != 0)
                {
                    LastCombinedProperties["TestKey"] = nestedStates[0];
                }

                LastMessage = base.RenderLogEvent(Layout, logEvent);
            }