public void ValidateDiagnosticsAppendContextConcurrentCalls() { int threadCount = 10; int itemCountPerThread = 100000; ConcurrentStack <Exception> concurrentStack = new ConcurrentStack <Exception>(); CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromSeconds(2)); } List <Thread> threads = new List <Thread>(threadCount); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(() => this.AddDiagnosticsInBackgroundLoop( itemCountPerThread, cosmosDiagnostics, concurrentStack)); thread.Start(); threads.Add(thread); } foreach (Thread thread in threads) { thread.Join(); } } Assert.AreEqual(0, concurrentStack.Count, $"Exceptions count: {concurrentStack.Count} Exceptions: {string.Join(';', concurrentStack)}"); int count = cosmosDiagnostics.Count(); Assert.AreEqual((threadCount * itemCountPerThread) + 1, count); }