Exemple #1
0
        public ILoggerFactory CreateLoggerFactory(GelfLoggerOptions options,
                                                  ActivityTrackingOptions trackingOptions = ActivityTrackingOptions.None)
        {
            var loggerFactory = LoggerFactory.Create(builder => builder
                                                     .Configure(loggerOptions => loggerOptions
                                                                .ActivityTrackingOptions = trackingOptions));

            loggerFactory.AddGelf(options);

            var logger = loggerFactory.CreateLogger <LoggerFixture>();

            logger.BeginScope(("test_id", TestContext.TestId));

            return(loggerFactory);
        }
Exemple #2
0
        /// <summary>
        /// Creates a new <see cref="LoggerFactory"/> instance.
        /// </summary>
        /// <param name="providers">The providers to use in producing <see cref="ILogger"/> instances.</param>
        /// <param name="filterOption">The filter option to use.</param>
        /// <param name="options">The <see cref="LoggerFactoryOptions"/>.</param>
        public LoggerFactory(IEnumerable <ILoggerProvider> providers, IOptionsMonitor <LoggerFilterOptions> filterOption, IOptions <LoggerFactoryOptions> options = null)
        {
            _factoryOptions = options == null || options.Value == null ? new LoggerFactoryOptions() : options.Value;

            const ActivityTrackingOptions ActivityTrackingOptionsMask = ~(ActivityTrackingOptions.SpanId | ActivityTrackingOptions.TraceId | ActivityTrackingOptions.ParentId |
                                                                          ActivityTrackingOptions.TraceFlags | ActivityTrackingOptions.TraceState);

            if ((_factoryOptions.ActivityTrackingOptions & ActivityTrackingOptionsMask) != 0)
            {
                throw new ArgumentException(SR.Format(SR.InvalidActivityTrackingOptions, _factoryOptions.ActivityTrackingOptions), nameof(options));
            }

            foreach (ILoggerProvider provider in providers)
            {
                AddProviderRegistration(provider, dispose: false);
            }

            _changeTokenRegistration = filterOption.OnChange(RefreshFilters);
            RefreshFilters(filterOption.CurrentValue);
        }
Exemple #3
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.
        }
Exemple #4
0
        private static string GetActivityLogString(ActivityTrackingOptions options)
        {
            Activity activity = Activity.Current;

            if (activity == null)
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            if ((options & ActivityTrackingOptions.SpanId) != 0)
            {
                sb.Append($"SpanId:{activity.GetSpanId()}");
            }

            if ((options & ActivityTrackingOptions.TraceId) != 0)
            {
                sb.Append(sb.Length > 0 ? $", TraceId:{activity.GetTraceId()}" : $"TraceId:{activity.GetTraceId()}");
            }

            if ((options & ActivityTrackingOptions.ParentId) != 0)
            {
                sb.Append(sb.Length > 0 ? $", ParentId:{activity.GetParentId()}" : $"ParentId:{activity.GetParentId()}");
            }

            if ((options & ActivityTrackingOptions.TraceState) != 0)
            {
                sb.Append(sb.Length > 0 ? $", TraceState:{activity.TraceStateString}" : $"TraceState:{activity.TraceStateString}");
            }

            if ((options & ActivityTrackingOptions.TraceFlags) != 0)
            {
                sb.Append(sb.Length > 0 ? $", TraceFlags:{activity.ActivityTraceFlags}" : $"TraceFlags:{activity.ActivityTraceFlags}");
            }

            return(sb.ToString());
        }
Exemple #5
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]);
        }
 public LoggerFactoryScopeProvider(ActivityTrackingOptions activityTrackingOption) => _activityTrackingOption = activityTrackingOption;