Skip to content

Add a callback on the reader options to expose the log filter switches #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -75,7 +75,8 @@ void ProcessFilterSwitchDeclarations()
SetFilterSwitch(throwOnError: true);
SubscribeToFilterExpressionChanges();

_resolutionContext.AddFilterSwitch(switchName, filterSwitch);
var referenceName = _resolutionContext.AddFilterSwitch(switchName, filterSwitch);
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(referenceName, filterSwitch);
Copy link
Contributor

@skomis-mm skomis-mm Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the $ sign make sense only for a sink parameter in appsettings.json to distinguish parameter value from variable reference:

"Filter": [
  {
    "Name": "ControlledBy",
    "Args": {
      "switch": "$filterSwitch"
    }
  } 
]

And since #231 $ is optional in declaration section, leaving name as is should look more consistent with overrides.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xced any thoughts on this one? (I'm working on merging everything through for a v7 shortly, would be great to get this PR in however this goes)

Copy link
Member Author

@0xced 0xced May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't forgotten about this. I will reply soon but I want to first experiment with a real world scenario to figure out what's best.

Copy link
Member Author

@0xced 0xced May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you that there's no need to force the leading $. So I addressed this in 7f5c27f.


void SubscribeToFilterExpressionChanges()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ 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.
/// The switch name includes the leading <c>$</c> character.
/// </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 @@ -1547,4 +1547,35 @@ public void TestLogLevelSwitchesCallback(string switchName)
var systemThreading = Assert.Contains("System.Threading", switches);
Assert.Equal(LogEventLevel.Debug, systemThreading.MinimumLevel);
}

[Theory]
[InlineData("$switch1", "$switch2")]
[InlineData("$switch1", "switch2")]
[InlineData("switch1", "$switch2")]
[InlineData("switch1", "switch2")]
public void TestLogFilterSwitchesCallback(string switch1Name, string switch2Name)
{
var json = $$"""
{
'Serilog': {
'FilterSwitches': {
'{{switch1Name}}': 'Prop = 1',
'{{switch2Name}}': '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; }
}
}