예제 #1
0
        public void CallsSetScopeProvider_OnSupportedProviders()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();
            var loggerFactory  = new LoggerFactory(new [] { loggerProvider });

            var logger = loggerFactory.CreateLogger("Logger");

            using (logger.BeginScope("Scope"))
            {
                using (logger.BeginScope("Scope2"))
                {
                    logger.LogInformation("Message");
                }
            }
            logger.LogInformation("Message2");

            Assert.Equal(loggerProvider.LogText,
                         new[]
            {
                "Message",
                "Scope",
                "Scope2",
                "Message2",
            });
            Assert.NotNull(loggerProvider.ScopeProvider);
            Assert.Equal(0, loggerProvider.BeginScopeCalledTimes);
        }
예제 #2
0
        public void TestActivityTrackingOptions_ShouldNotAddAdditionalScope_WhenTagsBaggageOptionAreSetButTagsAndBaggageAreEmpty()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.TraceId | ActivityTrackingOptions.Baggage | ActivityTrackingOptions.Tags)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.Start();
            logger.LogInformation("Message1");
            string traceIdActivityLogString = GetActivityLogString(ActivityTrackingOptions.TraceId);

            activity.Stop();

            Assert.Equal("Message1", loggerProvider.LogText[0]);
            Assert.Equal(traceIdActivityLogString, loggerProvider.LogText[1]);
            Assert.Equal(2, loggerProvider.LogText.Count); // Ensure that the additional scopes for tags and baggage aren't added.
        }
예제 #3
0
        public void BeginScope_ReturnsCompositeToken_ForMultipleLoggers()
        {
            var loggerProvider  = new ExternalScopeLoggerProvider();
            var loggerProvider2 = new InternalScopeLoggerProvider();
            var loggerFactory   = new LoggerFactory(new ILoggerProvider[] { loggerProvider, loggerProvider2 });

            var logger = loggerFactory.CreateLogger("Logger");

            using (logger.BeginScope("Scope"))
            {
                using (logger.BeginScope("Scope2"))
                {
                    logger.LogInformation("Message");
                }
            }
            logger.LogInformation("Message2");

            Assert.Equal(loggerProvider.LogText,
                         new[]
            {
                "Message",
                "Scope",
                "Scope2",
                "Message2",
            });

            Assert.Equal(loggerProvider2.LogText,
                         new[]
            {
                "Message",
                "Scope",
                "Scope2",
                "Message2",
            });
        }
예제 #4
0
        public void TestActivityTrackingOptions_ShouldAddTagsAndBaggageAsOneScopeAndTraceIdAsOtherScope_WhenTagsBaggageAndTraceIdOptionAreSet()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.TraceId | ActivityTrackingOptions.Baggage | ActivityTrackingOptions.Tags)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.AddTag("testTagKey1", "testTagValue");
            activity.AddBaggage("testBaggageKey1", "testBaggageValue");
            activity.Start();
            logger.LogInformation("Message1");
            string traceIdActivityLogString = GetActivityLogString(ActivityTrackingOptions.TraceId);

            activity.Stop();

            Assert.Equal("Message1", loggerProvider.LogText[0]);
            Assert.Equal(traceIdActivityLogString, loggerProvider.LogText[1]);
            Assert.Equal("testTagKey1:testTagValue", loggerProvider.LogText[2]);
            Assert.Equal("testBaggageKey1:testBaggageValue", loggerProvider.LogText[3]);
        }
예제 #5
0
        public void TestActivityTrackingOptions_ShouldAddTagsAsNewScope_WhenTagsOptionIsSet()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.TraceId | ActivityTrackingOptions.Tags)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.AddTag("testKey1", null);
            activity.AddTag("testKey2", string.Empty);
            activity.AddTag("testKey3", "testValue");
            activity.AddTag("testKey4", new Dummy());
            activity.Start();

            logger.LogInformation("Message1");

            activity.Stop();

            Assert.Equal("Message1", loggerProvider.LogText[0]);
            Assert.Equal("testKey1:, testKey2:, testKey3:testValue, testKey4:DummyToString", loggerProvider.LogText[2]);
        }
예제 #6
0
        public void TestActivityTrackingOptions_ShouldAddBaggageItemsAsNewScope_WhenBaggageOptionIsSet()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.Baggage)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.AddBaggage("testKey1", null);
            activity.AddBaggage("testKey2", string.Empty);
            activity.AddBaggage("testKey3", "testValue");
            activity.Start();

            logger.LogInformation("Message1");

            activity.Stop();

            foreach (string s in loggerProvider.LogText)
            {
                System.Console.WriteLine(s);
            }

            Assert.Equal("Message1", loggerProvider.LogText[0]);
            Assert.Equal("testKey3:testValue, testKey2:, testKey1:", loggerProvider.LogText[2]);
        }
예제 #7
0
        public void BeginScope_ReturnsExternalSourceTokenDirectly()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();
            var loggerFactory  = new LoggerFactory(new [] { loggerProvider });

            var logger = loggerFactory.CreateLogger("Logger");

            var scope = logger.BeginScope("Scope");

            Assert.StartsWith(loggerProvider.ScopeProvider.GetType().FullName, scope.GetType().FullName);
        }
예제 #8
0
        public void TestActivityIds(ActivityTrackingOptions options)
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = options)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.AddBaggage("baggageTestKey1", "baggageTestValue");
            activity.AddTag("tagTestKey", "tagTestValue");
            activity.Start();
            string activity1String = GetActivityLogString(options);
            string activity2String;

            using (logger.BeginScope("Scope 1"))
            {
                logger.LogInformation("Message 1");
                Activity b = new Activity("ScopeActivity");
                b.Start();
                activity2String = GetActivityLogString(options);

                using (logger.BeginScope("Scope 2"))
                {
                    logger.LogInformation("Message 2");
                }
                b.Stop();
            }
            activity.Stop();

            Assert.Equal(activity1String, loggerProvider.LogText[1]);
            Assert.Equal(activity2String, loggerProvider.LogText[4]);
            Assert.Equal(7, loggerProvider.LogText.Count); // Ensure that Baggage and Tags aren't added.
        }
예제 #9
0
        public void TestActivityIds(ActivityTrackingOptions options)
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = options)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity a = new Activity("ScopeActivity");

            a.Start();
            string activity1String = GetActivityLogString(options);
            string activity2String;

            using (logger.BeginScope("Scope 1"))
            {
                logger.LogInformation("Message 1");
                Activity b = new Activity("ScopeActivity");
                b.Start();
                activity2String = GetActivityLogString(options);

                using (logger.BeginScope("Scope 2"))
                {
                    logger.LogInformation("Message 2");
                }
                b.Stop();
            }
            a.Stop();

            Assert.Equal(activity1String, loggerProvider.LogText[1]);
            Assert.Equal(activity2String, loggerProvider.LogText[4]);
        }
예제 #10
0
        public void TestActivityTrackingOptions_ShouldAddNewTagAndBaggageItemsAtRuntime_WhenTagsAndBaggageOptionAreSetAndWithNestedScopes()
        {
            var loggerProvider = new ExternalScopeLoggerProvider();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .Configure(o => o.ActivityTrackingOptions = ActivityTrackingOptions.Baggage | ActivityTrackingOptions.Tags)
                .AddProvider(loggerProvider);
            });

            var logger = loggerFactory.CreateLogger("Logger");

            Activity activity = new Activity("ScopeActivity");

            activity.Start();

            // Add baggage and tag items before the first log entry.
            activity.AddTag("MyTagKey1", "1");
            activity.AddBaggage("MyBaggageKey1", "1");

            // Log a message, this should create any cached objects.
            logger.LogInformation("Message1");

            // Start the first scope, add some more items and log.
            using (logger.BeginScope("Scope1"))
            {
                activity.AddTag("MyTagKey2", "2");
                activity.AddBaggage("MyBaggageKey2", "2");
                logger.LogInformation("Message2");

                // Add two additional scopes and also replace some tag and baggage items.
                using (logger.BeginScope("Scope2"))
                {
                    activity.AddTag("MyTagKey3", "3");
                    activity.AddBaggage("MyBaggageKey3", "3");

                    using (logger.BeginScope("Scope3"))
                    {
                        activity.SetTag("MyTagKey3", "4");
                        activity.SetBaggage("MyBaggageKey3", "4");
                        logger.LogInformation("Message3");
                    }
                }

                // Along with this message we expect all baggage and tags items
                // as well as the Scope1 but not the Scope2 and Scope3.
                logger.LogInformation("Message4");

                activity.Stop();
            }

            Assert.Equal("Message1", loggerProvider.LogText[0]);
            Assert.Equal("MyTagKey1:1", loggerProvider.LogText[2]);
            Assert.Equal("MyBaggageKey1:1", loggerProvider.LogText[3]);

            Assert.Equal("Message2", loggerProvider.LogText[4]);
            Assert.Equal("MyTagKey1:1, MyTagKey2:2", loggerProvider.LogText[6]);
            Assert.Equal("MyBaggageKey2:2, MyBaggageKey1:1", loggerProvider.LogText[7]);
            Assert.Equal("Scope1", loggerProvider.LogText[8]);

            Assert.Equal("Message3", loggerProvider.LogText[9]);
            Assert.Equal("MyTagKey1:1, MyTagKey2:2, MyTagKey3:4", loggerProvider.LogText[11]);
            Assert.Equal("MyBaggageKey3:4, MyBaggageKey2:2, MyBaggageKey1:1", loggerProvider.LogText[12]);
            Assert.Equal("Scope1", loggerProvider.LogText[13]);
            Assert.Equal("Scope2", loggerProvider.LogText[14]);
            Assert.Equal("Scope3", loggerProvider.LogText[15]);

            Assert.Equal("Message4", loggerProvider.LogText[16]);
            Assert.Equal("MyTagKey1:1, MyTagKey2:2, MyTagKey3:4", loggerProvider.LogText[18]);
            Assert.Equal("MyBaggageKey3:4, MyBaggageKey2:2, MyBaggageKey1:1", loggerProvider.LogText[19]);
            Assert.Equal("Scope1", loggerProvider.LogText[20]);
        }