Пример #1
0
        public void NDCTop1TestTest()
        {
            LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets><target name='debug' type='Debug' layout='${ndc:topframes=1} ${message}' /></targets>
                <rules>
                    <logger name='*' minlevel='Debug' writeTo='debug' />
                </rules>
            </nlog>");

            NestedDiagnosticsContext.Clear();
            LogManager.GetLogger("A").Debug("0");
            AssertDebugLastMessage("debug", " 0");
            using (NestedDiagnosticsContext.Push("ala"))
            {
                LogManager.GetLogger("A").Debug("a");
                AssertDebugLastMessage("debug", "ala a");
                using (NestedDiagnosticsContext.Push("ma"))
                {
                    LogManager.GetLogger("A").Debug("b");
                    AssertDebugLastMessage("debug", "ma b");
                    using (NestedDiagnosticsContext.Push("kota"))
                    {
                        LogManager.GetLogger("A").Debug("c");
                        AssertDebugLastMessage("debug", "kota c");
                        NestedDiagnosticsContext.Push("kopytko");
                        LogManager.GetLogger("A").Debug("d");
                        AssertDebugLastMessage("debug", "kopytko d");
                        Assert.Equal("kopytko", NestedDiagnosticsContext.Pop()); // 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, NestedDiagnosticsContext.Pop());
            Assert.Equal(string.Empty, NestedDiagnosticsContext.TopMessage);
            NestedDiagnosticsContext.Push("zzz");
            Assert.Equal("zzz", NestedDiagnosticsContext.TopMessage);
            NestedDiagnosticsContext.Clear();
            Assert.Equal(string.Empty, NestedDiagnosticsContext.Pop());
            Assert.Equal(string.Empty, NestedDiagnosticsContext.TopMessage);
        }
Пример #2
0
        public void NDCTest1()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 500;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        NestedDiagnosticsContext.Clear();
                        Assert.AreEqual(string.Empty, NestedDiagnosticsContext.TopMessage);
                        Assert.AreEqual(string.Empty, NestedDiagnosticsContext.Pop());
                        AssertContents(NestedDiagnosticsContext.GetAllMessages());
                        using (NestedDiagnosticsContext.Push("foo"))
                        {
                            Assert.AreEqual("foo", NestedDiagnosticsContext.TopMessage);
                            AssertContents(NestedDiagnosticsContext.GetAllMessages(), "foo");
                            using (NestedDiagnosticsContext.Push("bar"))
                            {
                                AssertContents(NestedDiagnosticsContext.GetAllMessages(), "bar", "foo");
                                Assert.AreEqual("bar", NestedDiagnosticsContext.TopMessage);
                                NestedDiagnosticsContext.Push("baz");
                                AssertContents(NestedDiagnosticsContext.GetAllMessages(), "baz", "bar", "foo");
                                Assert.AreEqual("baz", NestedDiagnosticsContext.TopMessage);
                                Assert.AreEqual("baz", NestedDiagnosticsContext.Pop());

                                AssertContents(NestedDiagnosticsContext.GetAllMessages(), "bar", "foo");
                                Assert.AreEqual("bar", NestedDiagnosticsContext.TopMessage);
                            }

                            AssertContents(NestedDiagnosticsContext.GetAllMessages(), "foo");
                            Assert.AreEqual("foo", NestedDiagnosticsContext.TopMessage);
                        }

                        AssertContents(NestedDiagnosticsContext.GetAllMessages());
                        Assert.AreEqual(string.Empty, NestedDiagnosticsContext.Pop());
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            Assert.AreEqual(0, exceptions.Count);
        }
Пример #3
0
 /// <summary>
 ///   Pops the top message off the NDC stack.
 /// </summary>
 /// <returns>The top message which is no longer on the stack.</returns>
 public string Pop()
 {
     return(NestedDiagnosticsContext.Pop());
 }