Exemplo n.º 1
0
        public void AttachValue_AsyncTaskLevels_DoesntGoToOuterScope()
        {
            LogContext.Clear();
            var pair1 = new KeyValuePair <string, object>("1", 1);

            async Task Thing1()
            {
                await Thing2();

                CollectionAssert.AreEquivalent(new KeyValuePair <string, object> [0], LogContext.Current.GetValues());
            }

            async Task Thing2()
            {
                SetValue();
                CollectionAssert.AreEquivalent(new[] { pair1 }, LogContext.Current.GetValues());
                await Task.CompletedTask;
            }

            void SetValue()
            {
                LogContext.Current.AttachValue(pair1);
            }

            Thing1().GetAwaiter().GetResult();
        }
Exemplo n.º 2
0
        public async Task AttachValue_AsyncTaskOuterScope_DoesntGoToOuterScope()
        {
            LogContext.Clear();
            var pair1 = new KeyValuePair <string, object>("1", 1);
            var pair2 = new KeyValuePair <string, object>("2", 2);

            async Task Foo()
            {
                await Task.CompletedTask;

                LogContext.Current.AttachValue(pair2);
                await Task.CompletedTask;
            }

            await Foo();

            LogContext.Current.AttachValue(pair1);
            var actualValues   = LogContext.Current.GetValues();
            var expectedValues = new[] { pair1 };

            CollectionAssert.AreEquivalent(expectedValues, actualValues);
        }