/// <inheritdoc /> public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { var prop = new LogEventProperty( LogBufferContextPropertyName, new LogBufferScopeValue { LogBufferScope = LogBufferScope.EnsureCreated() } ); logEvent.AddOrUpdateProperty(prop); }
/// <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)); }