Пример #1
0
        static async Task Test_QueryMembershipSequential(ILogger serilog)
        {
            try
            {
                Console.WriteLine("\r\nSequential...");

                var proxy = GetProxy(serilog);
                for (int i = 0; i < 10; i++)
                {
                    // This renews the call chain ID.
                    TrackingContext.NewCurrent();

                    Debug.Assert(TrackingContext.Current != null);

                    string response = await proxy.RegisterMemberAsync(GetRegisterRequest(TrackingContext.Current.CallChainId.ToString())).ConfigureAwait(false);

                    Console.WriteLine(response);
                }

                Console.WriteLine("\r\nFinished...");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Test Exception: " + ex.Message);
            }
        }
Пример #2
0
        public async Task AsyncTrackingInterceptor_GivenTrackingContext_WhenReturnAsync_ThenSameTrackingContextReturned()
        {
            var instance    = new TestTrackingService();
            var interceptor = new AsyncTrackingInterceptor();

            ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor());

            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            TrackingContext returnedTrackingContext = null;
            await proxy.ReturnAsync(() =>
            {
                returnedTrackingContext = TrackingContext.Current;
            }).ConfigureAwait(false);

            TrackingContext.Current.Should().NotBeNull();
            TrackingContext.Current.CallChainId.Should().Be(currentTrackingContext.CallChainId);
            TrackingContext.Current.OriginatorUtcTimestamp.Should().Be(currentTrackingContext.OriginatorUtcTimestamp);

            returnedTrackingContext.Should().NotBeNull();
            returnedTrackingContext.CallChainId.Should().Be(currentTrackingContext.CallChainId);
            returnedTrackingContext.OriginatorUtcTimestamp.Should().Be(currentTrackingContext.OriginatorUtcTimestamp);
        }
Пример #3
0
        static void Test_QueryMembershipConcurrent(ILogger serilog)
        {
            try
            {
                Console.WriteLine("\r\nConcurrent...");

                var proxy = GetProxy(serilog);
                var tasks = new List <Task <string> >();
                for (int i = 0; i < 10; i++)
                {
                    // This renews the call chain ID.
                    TrackingContext.NewCurrent();

                    Debug.Assert(TrackingContext.Current != null);

                    Task <string> response = proxy.RegisterMemberAsync(GetRegisterRequest(TrackingContext.Current.CallChainId.ToString()));
                    tasks.Add(response);
                }
                Task.WaitAll(tasks.ToArray());

                foreach (var item in tasks)
                {
                    Console.WriteLine(item.Result);
                }

                Console.WriteLine("\r\nFinished...");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Test Exception: " + ex.Message);
            }
        }
        public async Task AsyncTrackingInterceptor_ReturnAsyncWithCurrent_SameTrackingContextReturned()
        {
            var instance    = new TestTrackingService();
            var interceptor = new AsyncTrackingInterceptor();

            ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor());

            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            Assert.IsNotNull(currentTrackingContext);

            TrackingContext returnedTrackingContext = null;
            await proxy.ReturnAsync(() =>
            {
                returnedTrackingContext = TrackingContext.Current;
            });

            Assert.IsNotNull(TrackingContext.Current);
            Assert.AreEqual(TrackingContext.Current.CallChainId, currentTrackingContext.CallChainId);
            Assert.AreEqual(TrackingContext.Current.OriginatorUtcTimestamp, currentTrackingContext.OriginatorUtcTimestamp);

            Assert.IsNotNull(returnedTrackingContext);
            Assert.AreEqual(currentTrackingContext.CallChainId, returnedTrackingContext.CallChainId);
            Assert.AreEqual(currentTrackingContext.OriginatorUtcTimestamp, returnedTrackingContext.OriginatorUtcTimestamp);
        }
Пример #5
0
        public void TrackingContextEnricher_GivenTrackingContext_WhenEnricherApplied_ThenPropertiesAdded()
        {
            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            logEvent.Properties.Count.Should().Be(2);
            logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString().Should().Be($"\"{currentTrackingContext.CallChainId}\"");
            logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString().Should().Be($"\"{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}\"");
        }
Пример #6
0
        public void TrackingContextEnricher_EnrichWithContext_PropertiesAdded()
        {
            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            Assert.IsNotNull(currentTrackingContext);

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            Assert.AreEqual(2, logEvent.Properties.Count);
            Assert.AreEqual($"\"{currentTrackingContext.CallChainId}\"", logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString());
            Assert.AreEqual($"\"{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}\"", logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString());
        }
Пример #7
0
        public void TrackingContextEnricher_GivenTrackingContextWithExtraHeaders_WhenEnricherApplied_ThenPropertiesAdded()
        {
            var extraHeaders = new Dictionary <string, string> {
                { "FirstKey", "FirstValue" },
                { "SecondKey", "SecondValue" }
            };

            TrackingContext.NewCurrent(extraHeaders);
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            logEvent.Properties.Count.Should().Be(4);
            logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString().Should().Be($@"""{currentTrackingContext.CallChainId}""");
            logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString().Should().Be($@"""{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}""");
            logEvent.Properties["FirstKey"].ToString().Should().Be(@"""FirstValue""");
            logEvent.Properties["SecondKey"].ToString().Should().Be(@"""SecondValue""");
        }
Пример #8
0
        public void TrackingContextEnricher_EnrichWithContextAndExtraHeaders_PropertiesAdded()
        {
            var extraHeaders = new Dictionary <string, string> {
                { "FirstKey", "FirstValue" },
                { "SecondKey", "SecondValue" }
            };

            TrackingContext.NewCurrent(extraHeaders);
            TrackingContext currentTrackingContext = TrackingContext.Current;

            Assert.IsNotNull(currentTrackingContext);

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            Assert.AreEqual(4, logEvent.Properties.Count);
            Assert.AreEqual($"\"{currentTrackingContext.CallChainId}\"", logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString());
            Assert.AreEqual($"\"{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}\"", logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString());
            Assert.AreEqual("\"FirstValue\"", logEvent.Properties["FirstKey"].ToString());
            Assert.AreEqual("\"SecondValue\"", logEvent.Properties["SecondKey"].ToString());
        }
Пример #9
0
            public static void Test(int i, ConcurrentStack <bool> results, ConcurrentStack <bool> nullChecks)
            {
                Task.Delay(100 * i).GetAwaiter().GetResult();

                TrackingContext.NewCurrent();

                Guid masterCallChainId = TrackingContext.Current.CallChainId;

                Task.Run(() =>
                {
                    Task.Delay(200 * i).GetAwaiter().GetResult();

                    TrackingContext.NewCurrent();

                    Guid newCallChainId = TrackingContext.Current.CallChainId;

                    results.Push(newCallChainId == masterCallChainId);
                    nullChecks.Push(newCallChainId == Guid.Empty);
                }).GetAwaiter().GetResult();

                Assert.IsNotNull(TrackingContext.Current);
                Assert.IsTrue(masterCallChainId == TrackingContext.Current.CallChainId);
            }
Пример #10
0
        public async Task TrackingContext_NestedMultipleTasks_CallChainIdCorresponds()
        {
            TrackingContext.NewCurrentIfEmpty();

            Guid masterCallChainId = TrackingContext.Current.CallChainId;

            bool result0  = false;
            bool result1  = false;
            bool result2  = false;
            bool result3  = false;
            bool result4  = false;
            bool result5  = false;
            bool result6  = false;
            bool result7  = false;
            bool result8  = false;
            bool result9  = false;
            bool result10 = false;
            bool result11 = false;

            await Task.Run(async() =>
            {
                TrackingContext tc0 = TrackingContext.Current;
                result0             = masterCallChainId == tc0.CallChainId;

                await Task.Run(async() =>
                {
                    TrackingContext tc1 = TrackingContext.Current;
                    result1             = masterCallChainId == tc1.CallChainId;

                    TrackingContext.NewCurrent();

                    TrackingContext tc2 = TrackingContext.Current;

                    result2 = tc1.CallChainId != tc2.CallChainId && tc2.CallChainId != Guid.Empty;

                    await Task.Run(() =>
                    {
                        TrackingContext tc3 = TrackingContext.Current;
                        result3             = tc2.CallChainId == tc3.CallChainId;

                        TrackingContext.NewCurrent();

                        TrackingContext tc4 = TrackingContext.Current;

                        result4 = tc3.CallChainId != tc4.CallChainId && tc4.CallChainId != Guid.Empty;
                    });

                    TrackingContext tc5 = TrackingContext.Current;

                    result5 = tc2.CallChainId == tc5.CallChainId;
                });

                await Task.Run(async() =>
                {
                    TrackingContext tc1 = TrackingContext.Current;
                    result6             = masterCallChainId == tc1.CallChainId;

                    TrackingContext.NewCurrent();

                    TrackingContext tc2 = TrackingContext.Current;

                    result7 = tc1.CallChainId != tc2.CallChainId && tc2.CallChainId != Guid.Empty;

                    await Task.Run(() =>
                    {
                        TrackingContext tc3 = TrackingContext.Current;
                        result8             = tc2.CallChainId == tc3.CallChainId;

                        TrackingContext.NewCurrent();

                        TrackingContext tc4 = TrackingContext.Current;

                        result9 = tc3.CallChainId != tc4.CallChainId && tc4.CallChainId != Guid.Empty;
                    });

                    TrackingContext tc5 = TrackingContext.Current;

                    result10 = tc2.CallChainId == tc5.CallChainId;
                });

                TrackingContext tc6 = TrackingContext.Current;
                result11            = tc0.CallChainId == tc6.CallChainId;
            });

            Assert.IsTrue(result0);
            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsTrue(result3);
            Assert.IsTrue(result4);
            Assert.IsTrue(result5);

            Assert.IsTrue(result6);
            Assert.IsTrue(result7);
            Assert.IsTrue(result8);
            Assert.IsTrue(result9);
            Assert.IsTrue(result10);
            Assert.IsTrue(result11);
            Assert.IsTrue(masterCallChainId == TrackingContext.Current.CallChainId);
        }
        public async Task TrackingContext_GivenNestedMultipleTasks_ThenCallChainIdCorresponds()
        {
            TrackingContext.NewCurrentIfEmpty();

            Guid masterCallChainId = TrackingContext.Current.CallChainId;

            bool result0  = false;
            bool result1  = false;
            bool result2  = false;
            bool result3  = false;
            bool result4  = false;
            bool result5  = false;
            bool result6  = false;
            bool result7  = false;
            bool result8  = false;
            bool result9  = false;
            bool result10 = false;
            bool result11 = false;

            await Task.Run(async() =>
            {
                TrackingContext tc0 = TrackingContext.Current;
                result0             = masterCallChainId == tc0.CallChainId;

                await Task.Run(async() =>
                {
                    TrackingContext tc1 = TrackingContext.Current;
                    result1             = masterCallChainId == tc1.CallChainId;

                    TrackingContext.NewCurrent();

                    TrackingContext tc2 = TrackingContext.Current;

                    result2 = tc1.CallChainId != tc2.CallChainId && tc2.CallChainId != Guid.Empty;

                    await Task.Run(() =>
                    {
                        TrackingContext tc3 = TrackingContext.Current;
                        result3             = tc2.CallChainId == tc3.CallChainId;

                        TrackingContext.NewCurrent();

                        TrackingContext tc4 = TrackingContext.Current;

                        result4 = tc3.CallChainId != tc4.CallChainId && tc4.CallChainId != Guid.Empty;
                    });

                    TrackingContext tc5 = TrackingContext.Current;

                    result5 = tc2.CallChainId == tc5.CallChainId;
                });

                await Task.Run(async() =>
                {
                    TrackingContext tc1 = TrackingContext.Current;
                    result6             = masterCallChainId == tc1.CallChainId;

                    TrackingContext.NewCurrent();

                    TrackingContext tc2 = TrackingContext.Current;

                    result7 = tc1.CallChainId != tc2.CallChainId && tc2.CallChainId != Guid.Empty;

                    await Task.Run(() =>
                    {
                        TrackingContext tc3 = TrackingContext.Current;
                        result8             = tc2.CallChainId == tc3.CallChainId;

                        TrackingContext.NewCurrent();

                        TrackingContext tc4 = TrackingContext.Current;

                        result9 = tc3.CallChainId != tc4.CallChainId && tc4.CallChainId != Guid.Empty;
                    });

                    TrackingContext tc5 = TrackingContext.Current;

                    result10 = tc2.CallChainId == tc5.CallChainId;
                });

                TrackingContext tc6 = TrackingContext.Current;
                result11            = tc0.CallChainId == tc6.CallChainId;
            });

            result0.Should().BeTrue();
            result1.Should().BeTrue();
            result2.Should().BeTrue();
            result3.Should().BeTrue();
            result4.Should().BeTrue();
            result5.Should().BeTrue();
            result6.Should().BeTrue();
            result7.Should().BeTrue();
            result8.Should().BeTrue();
            result9.Should().BeTrue();
            result10.Should().BeTrue();
            result11.Should().BeTrue();
            TrackingContext.Current.CallChainId.Should().Be(masterCallChainId);
        }