Ejemplo n.º 1
0
        public void NestedDiagnosticContextInformation_is_stored_in_the_same_sort_order_as_used_by_the_nested_diagnostic_layout_renderer()
        {
            var ndcSeparator  = "::";
            var ndlcSeparator = " ";

            ConfigureLogManager(ndcSeparator, ndlcSeparator);

            // Act
            NestedDiagnosticsContext.Clear();
            NestedDiagnosticsLogicalContext.Push("foo");
            NestedDiagnosticsLogicalContext.Push("bar");

            NestedDiagnosticsLogicalContext.Clear();
            NestedDiagnosticsContext.Push("baz1");
            NestedDiagnosticsContext.Push("baz2");
            NestedDiagnosticsContext.Push("baz3");

            var logEvent = Log("A", LogLevel.Debug, null, null, "some message");

            // Assert
            var expected = GetExpectedNdcValue(logEvent, ndcSeparator, ndlcSeparator);
            var actual   = GetXmlDocument().GetElementsByTagName("log4j:NDC")[0].InnerText;

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 2
0
        public void NDLCDeepTest()
        {
            LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndlc:topframes=1} ${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsLogicalContext.Clear();

            for (int i = 1; i <= 100; ++i)
            {
                NestedDiagnosticsLogicalContext.Push(i);
            }

            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", "100 0");

            NestedDiagnosticsLogicalContext.PopObject();
            LogManager.GetLogger("A").Debug("1");
            AssertDebugLastMessage("debug", "99 1");

            NestedDiagnosticsLogicalContext.Clear();
            LogManager.GetLogger("A").Debug("2");
            AssertDebugLastMessage("debug", " 2");
        }
Ejemplo n.º 3
0
        public void TargetWithContextNdlcSerializeTest()
        {
            NestedDiagnosticsLogicalContext.Clear();
            NestedDiagnosticsLogicalContext.Push(new { a = "b" });

            CustomTargetWithContext target = new CustomTargetWithContext()
            {
                IncludeNdlc = true, SkipAssert = true
            };

            WriteAndAssertSingleKey(target);
        }
Ejemplo n.º 4
0
        public void NDLCTop1TestTest()
        {
            LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndlc:topframes=1} ${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsLogicalContext.Clear();
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            using (NestedDiagnosticsLogicalContext.Push("ala"))
            {
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
                using (NestedDiagnosticsLogicalContext.Push("ma"))
                {
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ma b");
                    using (NestedDiagnosticsLogicalContext.Push("kota"))
                    {
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "kota c");
                        NestedDiagnosticsLogicalContext.Push("kopytko");
                        LogManager.GetLogger("A").Debug("d");
                        AssertDebugLastMessage("debug", "kopytko d");
                        Assert.Equal("kopytko", NestedDiagnosticsLogicalContext.PopObject()); // manual pop
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "kota c");
                    }
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ma b");
                }
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
            }
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            Assert.Null(NestedDiagnosticsLogicalContext.Pop()); //inconsistent with NDC - should be string.empty, but for backwardsscomp. Fix in NLog 5
            NestedDiagnosticsLogicalContext.Push("zzz");
            NestedDiagnosticsLogicalContext.Push("yyy");
            Assert.Equal("yyy", NestedDiagnosticsLogicalContext.Pop());
            NestedDiagnosticsLogicalContext.Clear();
            Assert.Null(NestedDiagnosticsLogicalContext.Pop()); //inconsistent with NDC - should be string.empty, but for backwardsscomp. Fix in NLog 5
        }
Ejemplo n.º 5
0
        public void NDLCTop1TestTest()
        {
            LogManager.Configuration = CreateConfigurationFromString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndlc:topframes=1} ${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsLogicalContext.Clear();
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            using (NestedDiagnosticsLogicalContext.Push("ala"))
            {
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
                using (NestedDiagnosticsLogicalContext.Push("ma"))
                {
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ma b");
                    using (NestedDiagnosticsLogicalContext.Push("kota"))
                    {
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "kota c");
                        NestedDiagnosticsLogicalContext.Push("kopytko");
                        LogManager.GetLogger("A").Debug("d");
                        AssertDebugLastMessage("debug", "kopytko d");
                        Assert.Equal("kopytko", NestedDiagnosticsLogicalContext.PopObject()); // manual pop
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "kota c");
                    }
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ma b");
                }
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
            }
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            Assert.Equal(string.Empty, NestedDiagnosticsLogicalContext.Pop());
            NestedDiagnosticsLogicalContext.Push("zzz");
            NestedDiagnosticsLogicalContext.Push("yyy");
            Assert.Equal("yyy", NestedDiagnosticsLogicalContext.Pop());
            NestedDiagnosticsLogicalContext.Clear();
            Assert.Equal(string.Empty, NestedDiagnosticsLogicalContext.Pop());
        }
Ejemplo n.º 6
0
        public void NdlcGetAllMessages()
        {
            object value = 5;

            NestedDiagnosticsLogicalContext.Clear();
            var popper = NestedDiagnosticsLogicalContext.Push(value);

            string expected = "5";

            string[] actual = NestedDiagnosticsLogicalContext.GetAllMessages();
            Assert.Single(actual);
            Assert.Equal(expected, actual[0]);

            popper.Dispose();
            actual = NestedDiagnosticsLogicalContext.GetAllMessages();
            Assert.Empty(actual);
        }
Ejemplo n.º 7
0
        public void LegacyNdlcClearShouldNotAffectProperties1()
        {
            // Arrange
            ScopeContext.Clear();
            var    expectedValue = "World";
            var    success       = false;
            object propertyValue;

            // Act
            using (ScopeContext.PushProperty("Hello", expectedValue))
            {
                NestedDiagnosticsLogicalContext.Clear();    // Should not clear anything (skip legacy mode)
                success = ScopeContext.TryGetProperty("Hello", out propertyValue);
            }

            // Assert
            Assert.True(success);
            Assert.Equal(expectedValue, propertyValue);
        }
Ejemplo n.º 8
0
        public void NDLCTest()
        {
            LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndlc} ${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsLogicalContext.Clear();
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            using (NestedDiagnosticsLogicalContext.Push("ala"))
            {
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
                using (NestedDiagnosticsLogicalContext.Push("ma"))
                {
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ala ma b");
                    using (NestedDiagnosticsLogicalContext.Push("kota"))
                    {
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "ala ma kota c");
                        using (NestedDiagnosticsLogicalContext.Push("kopytko"))
                        {
                            LogManager.GetLogger("A").Debug("d");
                            AssertDebugLastMessage("debug", "ala ma kota kopytko d");
                        }
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "ala ma kota c");
                    }
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ala ma b");
                }
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
            }
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
        }
Ejemplo n.º 9
0
        public void If_Ndc_And_Ndlc_is_present_they_are_combined_with_a_NdcSeparator()
        {
            // Arrange
            var ndcSeparator  = "::";
            var ndlcSeparator = " ";

            ConfigureLogManager(ndcSeparator, ndlcSeparator);

            // Act
            NestedDiagnosticsContext.Clear();
            NestedDiagnosticsContext.Push("foo");

            NestedDiagnosticsLogicalContext.Clear();
            NestedDiagnosticsLogicalContext.Push("bar");

            var logEvent = Log("A", LogLevel.Debug, null, null, "some message");

            // Assert
            var expected = GetExpectedNdcValue(logEvent, ndcSeparator, ndlcSeparator);
            var actual   = GetXmlDocument().GetElementsByTagName("log4j:NDC")[0].InnerText;

            Assert.Equal(expected, actual);
        }
 public void TearDown()
 {
     _events = null;
     NestedDiagnosticsLogicalContext.Clear();
 }
Ejemplo n.º 11
0
        public void NDLCTimingTest()
        {
            LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndlc}|${ndlctiming:CurrentScope=false:ScopeBeginTime=true:Format=yyyy-MM-dd HH\:mm\:ss}|${ndlctiming:CurrentScope=false:ScopeBeginTime=false:Format=fff}|${ndlctiming:CurrentScope=true:ScopeBeginTime=true:Format=HH\:mm\:ss.fff}|${ndlctiming:CurrentScope=true:ScopeBeginTime=false:Format=fffffff}|${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsLogicalContext.Clear();
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", "|||||0");
            using (NestedDiagnosticsLogicalContext.Push("ala"))
            {
                LogManager.GetLogger("A").Debug("a");
                var measurements = GetDebugLastMessage("debug").Split(new[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                Assert.Equal(6, measurements.Length);
                Assert.Equal("ala", measurements[0]);
                Assert.InRange(int.Parse(measurements[2]), 0, 999);
                Assert.InRange(int.Parse(measurements[4]), 0, 9999999);
                Assert.Equal("a", measurements[measurements.Length - 1]);

                System.Threading.Thread.Sleep(10);

                LogManager.GetLogger("A").Debug("b");
                measurements = GetDebugLastMessage("debug").Split(new[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                Assert.Equal("ala", measurements[0]);
                Assert.InRange(int.Parse(measurements[2]), 10, 999);
                Assert.InRange(int.Parse(measurements[4]), 100000, 9999999);
                Assert.Equal("b", measurements[measurements.Length - 1]);

                using (NestedDiagnosticsLogicalContext.Push("ma"))
                {
                    LogManager.GetLogger("A").Debug("a");
                    measurements = GetDebugLastMessage("debug").Split(new[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                    Assert.Equal(6, measurements.Length);
                    Assert.Equal("ala ma", measurements[0]);
                    Assert.InRange(int.Parse(measurements[2]), 10, 999);
                    Assert.InRange(int.Parse(measurements[4]), 0, 9999999);
                    Assert.Equal("a", measurements[measurements.Length - 1]);

                    System.Threading.Thread.Sleep(10);

                    LogManager.GetLogger("A").Debug("b");
                    measurements = GetDebugLastMessage("debug").Split(new[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                    Assert.Equal(6, measurements.Length);
                    Assert.Equal("ala ma", measurements[0]);
                    Assert.InRange(int.Parse(measurements[2]), 20, 999);
                    Assert.InRange(int.Parse(measurements[4]), 100000, 9999999);
                    Assert.Equal("b", measurements[measurements.Length - 1]);
                }

                LogManager.GetLogger("A").Debug("c");
                measurements = GetDebugLastMessage("debug").Split(new[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                Assert.Equal("ala", measurements[0]);
                Assert.InRange(int.Parse(measurements[2]), 20, 999);
                Assert.InRange(int.Parse(measurements[4]), 200000, 9999999);
                Assert.Equal("c", measurements[measurements.Length - 1]);
            }

            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", "|||||0");
        }