Example #1
0
        public void LegacyMdlcSetShouldNotAffectStackValues2()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedValue       = "World";
            var    expectedNestedState = "First Push";
            object propertyValue;

            object[] allNestedStates = null;
            var      success         = false;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState))
            {
                using (ScopeContext.PushProperty("Hello", expectedValue))
                {
                    MappedDiagnosticsLogicalContext.Set("Hello", expectedValue);    // Skip legacy mode (ignore when same value)
                    success         = ScopeContext.TryGetProperty("Hello", out propertyValue);
                    allNestedStates = ScopeContext.GetAllNestedStates();
                }
            }

            // Assert
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
            Assert.True(success);
            Assert.Equal(expectedValue, propertyValue);
        }
Example #2
0
        public void LegacyMdlcSetShouldNotAffectStackValues3()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedValue       = "Bob";
            var    expectedNestedState = "First Push";
            object propertyValue1;
            object propertyValue2;

            object[] allNestedStates = null;
            var      success1        = false;
            var      success2        = false;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState))
            {
                using (ScopeContext.PushProperty("Hello", "World"))
                {
                    MappedDiagnosticsLogicalContext.Set("Hello", expectedValue);    // Enter legacy mode (need to overwrite)
                    success1        = ScopeContext.TryGetProperty("Hello", out propertyValue1);
                    allNestedStates = ScopeContext.GetAllNestedStates();
                }

                success2 = ScopeContext.TryGetProperty("Hello", out propertyValue2);
            }

            // Assert
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
            Assert.True(success1);
            Assert.Equal(expectedValue, propertyValue1);
            Assert.False(success2);
            Assert.Null(propertyValue2);
        }
Example #3
0
        public void DoublePushNestedStateTest()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedNestedState1 = "First Push";
            var    expectedNestedState2 = System.Guid.NewGuid();
            object topNestedState1      = null;
            object topNestedState2      = null;

            object[] allNestedStates = null;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState1))
            {
                topNestedState1 = ScopeContext.PeekNestedState();
                using (ScopeContext.PushNestedState(expectedNestedState2))
                {
                    topNestedState2 = ScopeContext.PeekNestedState();
                    allNestedStates = ScopeContext.GetAllNestedStates();
                }
            }
            var failed = ScopeContext.PeekNestedState() != null;

            // Assert
            Assert.Equal(expectedNestedState1, topNestedState1);
            Assert.Equal(expectedNestedState2, topNestedState2);
            Assert.Equal(2, allNestedStates.Length);
            Assert.Equal(expectedNestedState2, allNestedStates[0]);
            Assert.Equal(expectedNestedState1, allNestedStates[1]);
            Assert.False(failed);
        }
Example #4
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 #5
0
        public void LegacyMdlcRemoveShouldNotAffectStackValues1()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedNestedState = "First Push";

            object[] allNestedStates = null;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState))
            {
                MappedDiagnosticsLogicalContext.Remove("Hello");    // Should not remove anything (skip legacy mode)
                allNestedStates = ScopeContext.GetAllNestedStates();
            }

            // Assert
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
        }
Example #6
0
        public void LegacyMdlcRemoveShouldNotAffectStackValues2()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedValue1       = "World";
            var    expectedValue2       = System.Guid.NewGuid();
            var    expectedNestedState1 = "First Push";
            var    expectedNestedState2 = System.Guid.NewGuid();
            object propertyValue1;
            object propertyValue2;

            object[] allNestedStates = null;
            var      success1        = false;
            var      success2        = false;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState1))
            {
                using (ScopeContext.PushProperty("Hello", expectedValue1))
                {
                    using (ScopeContext.PushNestedState(expectedNestedState2))
                    {
                        ScopeContext.PushProperty("RequestId", expectedValue2);
                        MappedDiagnosticsLogicalContext.Remove("RequestId");    // Should not change stack (Legacy mode)
                        allNestedStates = ScopeContext.GetAllNestedStates();

                        success1 = ScopeContext.TryGetProperty("Hello", out propertyValue1);
                        success2 = ScopeContext.TryGetProperty("RequestId", out propertyValue2);
                    }
                }
            }

            // Assert
            Assert.Equal(2, allNestedStates.Length);
            Assert.Equal(expectedNestedState2, allNestedStates[0]);
            Assert.Equal(expectedNestedState1, allNestedStates[1]);
            Assert.True(success1);
            Assert.False(success2);
            Assert.Equal(expectedValue1, propertyValue1);
            Assert.Null(propertyValue2);
        }
Example #7
0
        public void LegacyMdlcClearShouldNotAffectStackValues2()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedValue       = "World";
            var expectedNestedState = "First Push";

            object[] allNestedStates = null;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState))
            {
                ScopeContext.PushProperty("Hello", expectedValue);
                MappedDiagnosticsLogicalContext.Clear();    // Should not clear stack (Legacy mode)
                allNestedStates = ScopeContext.GetAllNestedStates();
            }

            // Assert
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
        }
Example #8
0
        public void PushNestedStateTest()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedNestedState = "First Push";
            object topNestedState      = null;

            object[] allNestedStates = null;

            // Act
            using (ScopeContext.PushNestedState(expectedNestedState))
            {
                topNestedState  = ScopeContext.PeekNestedState();
                allNestedStates = ScopeContext.GetAllNestedStates();
            }
            var failed = ScopeContext.PeekNestedState() != null;

            // Assert
            Assert.Equal(expectedNestedState, topNestedState);
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
            Assert.False(failed);
        }
Example #9
0
        public void ClearScopeContextTest()
        {
            // Arrange
            ScopeContext.Clear();
            var expectedNestedState = "First Push";
            var expectedString      = "World";
            var expectedGuid        = System.Guid.NewGuid();

            object[] allNestedStates1   = null;
            object[] allNestedStates2   = null;
            object   stringValueLookup1 = null;
            object   stringValueLookup2 = null;

            // Act
            using (ScopeContext.PushProperty("Hello", expectedString))
            {
                using (ScopeContext.PushProperty("RequestId", expectedGuid))
                {
                    using (ScopeContext.PushNestedState(expectedNestedState))
                    {
                        ScopeContext.Clear();
                        allNestedStates1 = ScopeContext.GetAllNestedStates();
                        ScopeContext.TryGetProperty("Hello", out stringValueLookup1);
                    }
                }

                // Original scope was restored on dispose, verify expected behavior
                allNestedStates2 = ScopeContext.GetAllNestedStates();
                ScopeContext.TryGetProperty("Hello", out stringValueLookup2);
            }

            // Assert
            Assert.Null(stringValueLookup1);
            Assert.Equal(expectedString, stringValueLookup2);
            Assert.Empty(allNestedStates1);
            Assert.Empty(allNestedStates2);
        }
Example #10
0
        public void LoggerPushNestedStatePrimitiveTest()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedNestedState = 42;
            object topNestedState      = null;

            object[] allNestedStates = null;
            var      logger          = new LogFactory().GetCurrentClassLogger();

            // Act
            using (logger.PushScopeState(expectedNestedState))
            {
                topNestedState  = ScopeContext.PeekNestedState();
                allNestedStates = ScopeContext.GetAllNestedStates();
            }
            var failed = ScopeContext.PeekNestedState() != null;

            // Assert
            Assert.Equal(expectedNestedState, topNestedState);
            Assert.Single(allNestedStates);
            Assert.Equal(expectedNestedState, allNestedStates[0]);
            Assert.False(failed);
        }
Example #11
0
            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);
            }
 public static object[] GetAllObjects()
 {
     return(ScopeContext.GetAllNestedStates());
 }
Example #13
0
        /// <summary>
        /// Renders the specified Nested Logical Context item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            if (TopFrames == 1)
            {
                // Allows fast rendering of topframes=1
                var topFrame = ScopeContext.PeekNestedState();
                if (topFrame != null)
                {
                    builder.AppendFormattedValue(topFrame, Format, GetFormatProvider(logEvent), ValueFormatter);
                }
                return;
            }

            var messages = ScopeContext.GetAllNestedStates();

            if (messages.Length == 0)
            {
                return;
            }

            int startPos = 0;
            int endPos   = messages.Length;

            if (TopFrames != -1)
            {
                endPos = Math.Min(TopFrames, messages.Length);
            }
            else if (BottomFrames != -1)
            {
                startPos = messages.Length - Math.Min(BottomFrames, messages.Length);
            }

            string separator     = Separator?.Render(logEvent) ?? string.Empty;
            string itemSeparator = separator;

            if (Format == MessageTemplates.ValueFormatter.FormatAsJson)
            {
                builder.Append("[");
                builder.Append(separator);
                itemSeparator = "," + separator;
            }

            try
            {
                var    formatProvider   = GetFormatProvider(logEvent);
                string currentSeparator = string.Empty;
                for (int i = endPos - 1; i >= startPos; --i)
                {
                    builder.Append(currentSeparator);
                    AppendFormattedValue(messages[i], formatProvider, builder, separator, itemSeparator);
                    currentSeparator = itemSeparator;
                }
            }
            finally
            {
                if (Format == MessageTemplates.ValueFormatter.FormatAsJson)
                {
                    builder.Append(separator);
                    builder.Append("]");
                }
            }
        }