public void NDCTest2_object() { List <Exception> exceptions = new List <Exception>(); ManualResetEvent mre = new ManualResetEvent(false); int counter = 100; int remaining = counter; for (int i = 0; i < counter; ++i) { ThreadPool.QueueUserWorkItem( s => { try { NestedDiagnosticsContext.Clear(); Assert.Null(NestedDiagnosticsContext.TopObject); Assert.Null(NestedDiagnosticsContext.PopObject()); AssertContents(NestedDiagnosticsContext.GetAllMessages()); using (NestedDiagnosticsContext.Push("foo")) { Assert.Equal("foo", NestedDiagnosticsContext.TopObject); AssertContents(NestedDiagnosticsContext.GetAllObjects(), "foo"); using (NestedDiagnosticsContext.Push("bar")) { AssertContents(NestedDiagnosticsContext.GetAllObjects(), "bar", "foo"); Assert.Equal("bar", NestedDiagnosticsContext.TopObject); NestedDiagnosticsContext.Push("baz"); AssertContents(NestedDiagnosticsContext.GetAllObjects(), "baz", "bar", "foo"); Assert.Equal("baz", NestedDiagnosticsContext.TopObject); Assert.Equal("baz", NestedDiagnosticsContext.PopObject()); AssertContents(NestedDiagnosticsContext.GetAllObjects(), "bar", "foo"); Assert.Equal("bar", NestedDiagnosticsContext.TopObject); } AssertContents(NestedDiagnosticsContext.GetAllObjects(), "foo"); Assert.Equal("foo", NestedDiagnosticsContext.TopObject); } AssertContents(NestedDiagnosticsContext.GetAllMessages()); Assert.Null(NestedDiagnosticsContext.PopObject()); } catch (Exception ex) { lock (exceptions) { exceptions.Add(ex); } } finally { if (Interlocked.Decrement(ref remaining) == 0) { mre.Set(); } } }); } mre.WaitOne(); StringBuilder exceptionsMessage = new StringBuilder(); foreach (var ex in exceptions) { if (exceptionsMessage.Length > 0) { exceptionsMessage.Append("\r\n"); } exceptionsMessage.Append(ex.ToString()); } Assert.True(exceptions.Count == 0, exceptionsMessage.ToString()); }