Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ Using this package you can also declare `LoggingFilterSwitch`-es in custom secti

Level updates to switches are also respected for a dynamic update.

Since version 4.0.0, filter switches are exposed through a callback on the reader options so that a reference can be kept:

```csharp
var filterSwitches = new Dictionary<string, ILoggingFilterSwitch>();
var options = new ConfigurationReaderOptions
{
OnFilterSwitchCreated = (switchName, filterSwitch) => filterSwitches[switchName] = filterSwitch
};

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();

ILoggingFilterSwitch filterSwitch = filterSwitches["filterSwitch"];
```

### Nested configuration sections

Some Serilog packages require a reference to a logger configuration object. The sample program in this project illustrates this with the following entry configuring the _[Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async)_ package to wrap the _[Serilog.Sinks.File](https://github.com/serilog/serilog-sinks-file)_ package. The `configure` parameter references the File sink configuration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void ProcessFilterSwitchDeclarations()
SubscribeToFilterExpressionChanges();

_resolutionContext.AddFilterSwitch(switchName, filterSwitch);
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(switchName, filterSwitch);

void SubscribeToFilterExpressionChanges()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
/// </summary>
public Action<string, LoggingLevelSwitch>? OnLevelSwitchCreated { get; init; }

/// <summary>
/// Called when a log filter switch is created while reading the <c>Serilog:FilterSwitches</c> section of the configuration.
/// </summary>
public Action<string, ILoggingFilterSwitch>? OnFilterSwitchCreated { get; init; }

internal Assembly[]? Assemblies { get; }
internal DependencyContext? DependencyContext { get; }
internal ConfigurationAssemblySource? ConfigurationAssemblySource { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Serilog.Settings.Configuration;

/// <summary>
/// A log event filter that can be modified at runtime.
/// </summary>
/// <remarks>
/// Under the hood, the logging filter switch is either a <c>Serilog.Expressions.LoggingFilterSwitch</c> or a <c>Serilog.Filters.Expressions.LoggingFilterSwitch</c> instance.
/// </remarks>
public interface ILoggingFilterSwitch
{
/// <summary>
/// A filter expression against which log events will be tested.
/// Only expressions that evaluate to <c>true</c> are included by the filter. A <c>null</c> expression will accept all events.
/// </summary>
public string? Expression { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Serilog.Settings.Configuration;

class LoggingFilterSwitchProxy
class LoggingFilterSwitchProxy : ILoggingFilterSwitch
{
readonly Action<string?> _setProxy;
readonly Func<string?> _getProxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ public string AddLevelSwitch(string levelSwitchName, LoggingLevelSwitch levelSwi
return referenceName;
}

public string AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
public void AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
{
if (filterSwitchName == null) throw new ArgumentNullException(nameof(filterSwitchName));
if (filterSwitch == null) throw new ArgumentNullException(nameof(filterSwitch));
var referenceName = ToSwitchReference(filterSwitchName);
_declaredFilterSwitches[referenceName] = filterSwitch;
return referenceName;
}

string ToSwitchReference(string switchName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,4 +1547,32 @@ public void TestLogLevelSwitchesCallback(string switchName)
var systemThreading = Assert.Contains("System.Threading", switches);
Assert.Equal(LogEventLevel.Debug, systemThreading.MinimumLevel);
}

[Fact]
public void TestLogFilterSwitchesCallback()
{
// language=json
var json = """
{
"Serilog": {
"FilterSwitches": {
"switch1": "Prop = 1",
"$switch2": "Prop = 2"
}
}
}
""";

IDictionary<string, ILoggingFilterSwitch> switches = new Dictionary<string, ILoggingFilterSwitch>();
var readerOptions = new ConfigurationReaderOptions { OnFilterSwitchCreated = (name, filterSwitch) => switches[name] = filterSwitch };
ConfigFromJson(json, options: readerOptions);

Assert.Equal(2, switches.Count);

var switch1 = Assert.Contains("switch1", switches);
Assert.Equal("Prop = 1", switch1.Expression);

var switch2 = Assert.Contains("$switch2", switches);
Assert.Equal("Prop = 2", switch2.Expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ namespace Serilog.Settings.Configuration
public bool AllowInternalMethods { get; init; }
public bool AllowInternalTypes { get; init; }
public System.IFormatProvider? FormatProvider { get; init; }
public System.Action<string, Serilog.Settings.Configuration.ILoggingFilterSwitch>? OnFilterSwitchCreated { get; init; }
public System.Action<string, Serilog.Core.LoggingLevelSwitch>? OnLevelSwitchCreated { get; init; }
public string? SectionName { get; init; }
}
public interface ILoggingFilterSwitch
{
string? Expression { get; set; }
}
}