-
Notifications
You must be signed in to change notification settings - Fork 1
Log Filtering: Package v.3.x
The third version of the library radically changes the log filtering mechanism presented in the second version for even greater flexibility and efficiency. Instead of implementing filters as classes that implement an interface IFilter, you can use a FluentAPI builder to create a filtering predicate that will be applied to the logs.
LogsFiltersConfiguration is the central class of the package. It represents the configuration necessary for applying log filters according to specified rules.
-
ApplyLogFilters: A
boolproperty that gets or initializes a value indicating whether log filters should be applied. If set totrue, the filtering rules defined in theQueryBuilderwill be used for the logs. -
QueryBuilder: An optional property of type
ILogQueryBuilder. It allows the definition of complex filtering rules through a fluent API. If not set, no filters will be applied.
ILogQueryBuilder serves as the foundation for building log filtering queries. It offers methods to define filters based on message content, exceptions, custom properties, and log levels.
-
Message: Gets an
IMessageRuleBuilderto define rules based on the log message. -
Exception: Gets an
IExceptionRuleBuilderto define rules based on exceptions logged. -
Properties: Gets an
IPropertiesRuleBuilderto define rules based on custom properties in the log entry. -
Level: Gets an
ILevelRuleBuilderto define rules based on the log level. -
And(): Returns an
ILogQueryBuilderfor combining conditions with a logical AND. -
Or(): Returns an
ILogQueryBuilderfor combining conditions with a logical OR.
These interfaces inherit from ILogQueryBuilder and provide specific methods for defining rules based on different aspects of log entries:
- Contains: Adds a condition for entries that contain the specified substring.
- NotContains: Adds a condition for entries that do not contain the specified substring.
-
Equals: Adds a condition for entries that exactly match the specified value. For
IMessageRuleBuilderandIExceptionRuleBuilder, the comparison is against the message content. ForIPropertiesRuleBuilder, it matches against a key-value pair in the log entry's properties. - NotEquals: Adds a condition for entries that do not match the specified value.
- Null: Adds a condition for entries where the specified field is null.
- NotNull: Adds a condition for entries where the specified field is not null.
-
InRange (
ILevelRuleBuilderonly): This function adds a condition for entries within a specified log level range. -
NotInRange (
ILevelRuleBuilderonly): Adds a condition for entries outside a specified log level range.
var config = new LogsFiltersConfiguration
{
ApplyLogFilters = true,
QueryBuilder = new LogQueryBuilder()
.Message.Contains("error")
.And()
.Properties.NotEquals("user", "admin", StringComparison.OrdinalIgnoreCase)
.Or()
.Level.InRange(LogEventLevel.Warning, LogEventLevel.Fatal)
};In this example, the LogsFiltersConfiguration is set up to apply filters that include log entries containing the substring "error" in their message, but not from a user named "admin", or with a log level within the range from Warning to Fatal.
To check the filter locally without running the complete logger, you can use the following code:
var filteringPredicate = LogQueryBuilder.Create()
.Exception.NotContains("ExampleException") as IFilterExecutor;
var log = new LogEvent(
timestamp: DateTimeOffset.Now,
level: LogEventLevel.Fatal,
exception: GetTestException(),
messageTemplate: new MessageTemplate([]),
properties: []
);
Console.WriteLine("Exception pass the filter: {0}", filteringPredicate!.Evaluate(log));
return;
Exception GetTestException()
=> new("ExampleException!: " +
"This is a test exception to evaluate filter behavior");