コード例 #1
0
            /// <inheritdoc />
            public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
            {
                var prop = new LogEventProperty(
                    LogBufferContextPropertyName,
                    new LogBufferScopeValue {
                    LogBufferScope = LogBufferScope.EnsureCreated()
                }
                    );

                logEvent.AddOrUpdateProperty(prop);
            }
コード例 #2
0
        public void LogEvent(LogEvent logEvent, bool writeToSink, BufferSinkConfig config)
        {
            CapturedConfig = config;
            var shouldTrigger = config.TriggerEventLevel.IsSatisfiedBy(logEvent);

            if (IsTriggered || writeToSink)
            {
                config.InnerSink.Emit(logEvent);
            }

            if (shouldTrigger)
            {
                // Notify the LogBufferScope of the triggering event
                LogBufferScope.Trigger();
            }

            if (!IsTriggered && shouldTrigger)
            {
                TriggerFlush();
            }
            else if (!IsTriggered && !writeToSink)
            {
                Bag.Add(logEvent);

                if (config.BufferCapacity > 0 && Bag.Count > config.BufferCapacity)
                {
                    PruneLogEvents();
                }
            }

            void PruneLogEvents()
            {
                lock (this)
                {
                    var logEvents = Bag.ToArray().OrderBy(x => x.Timestamp);

                    // Create new Bag with half the items
                    Bag = new ConcurrentBag <LogEvent>(logEvents.Take(Math.Max(1, config.BufferCapacity / 2)));
                }
            }
        }
コード例 #3
0
 public LogBuffer(LogBufferScope logBufferScope)
 {
     LogBufferScope = logBufferScope;
 }
コード例 #4
0
        /// <summary>
        ///     Add output configuration for the the buffered sink.
        /// </summary>
        /// <param name="configureOutput">Logger configuration setup to handle writing events to outputs.</param>
        public BufferSinkLoggerConfiguration WriteTo(Action <LoggerConfiguration> configureOutput)
        {
            // Create a LogBufferScope as early as possible to capture the most general AsyncLocal context
            // TODO: Replace this with a global static scope that is parent to all orphan scopes
            LogBufferScope.EnsureCreated();

            var userLc = new LoggerConfiguration().MinimumLevel.Verbose();

            configureOutput.Invoke(userLc);
            var outputSink = userLc.CreateLogger();

            LoggerConfiguration.MinimumLevel.Verbose().Enrich.WithBufferContext();

            bool IsFallbackSourceConfig(SourceConfig s)
            {
                return(string.IsNullOrEmpty(s.SourceToMatchOn) || s.SourceToMatchOn == "*");
            }

            var sourceConfigs  = SourceConfigs.Where(x => !IsFallbackSourceConfig(x)).ToList();
            var fallbackConfig = SourceConfigs.LastOrDefault(IsFallbackSourceConfig);

            // Create source configs and fallback for the BufferSink
            var bufferConfigs = sourceConfigs.Select(
                x => new BufferSink.SourceConfig_Internal(
                    Matching.FromSource(x.SourceToMatchOn), x.MinLevelAlways, null
                    )
                )
                                .ToList();
            var bufferFallbackConfig = fallbackConfig != null
                ? new BufferSink.SourceConfig_Internal(_ => true, fallbackConfig.MinLevelAlways, FallbackLevelSwitch)
                : null;

            // Create BufferSink with configs and outputSink
            var bufferSink = new BufferSink(
                new BufferSinkConfig(TriggeringLevel, outputSink, BufferCapacity), bufferConfigs, bufferFallbackConfig
                );

            foreach (var c in sourceConfigs)
            {
                // Events matching a source config will obey the lower of the two minimum levels
                var minimumLevel = c.MinLevelAlways < c.MinLevelOnDetailed ? c.MinLevelAlways : c.MinLevelOnDetailed;
                // NB: MinimumLevel.Override not usable in sub-loggers, see Serilog issue #967
                // We implement the equivalent filter
                LoggerConfiguration.WriteTo.Logger(
                    l => l.Filter.ByIncludingOnly(Matching.FromSource(c.SourceToMatchOn))
                    .MinimumLevel.Is(minimumLevel)
                    .WriteTo.Sink(bufferSink)
                    );
            }

            // Handle anything without a matching source config
            LoggerConfiguration.WriteTo.Logger(
                l => {
                // Exclude events that match any config
                foreach (var c in sourceConfigs)
                {
                    l.Filter.ByExcluding(Matching.FromSource(c.SourceToMatchOn));
                }

                if (fallbackConfig != null)
                {
                    // Events matching a source config will obey the lower of the two minimum levels
                    l.MinimumLevel.Is(
                        fallbackConfig.MinLevelAlways < fallbackConfig.MinLevelOnDetailed
                                ? fallbackConfig.MinLevelAlways
                                : fallbackConfig.MinLevelOnDetailed
                        );

                    l.WriteTo.Sink(bufferSink);
                }
                else
                {
                    // No fallback config given, so write to output immediately
                    l.WriteTo.Sink(outputSink);
                }
            }
                );

            return(new BufferSinkLoggerConfiguration(LoggerConfiguration));
        }