Skip to content

Commit 24b4f06

Browse files
authored
Merge pull request #24 from Bardin08/vbardin/logs-filters
Implement fluent api based logs filters
2 parents af7a8da + 1c3364c commit 24b4f06

18 files changed

+346
-56
lines changed

examples/ConsoleApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
while (logsCounter <= logsThreshold)
1919
{
2020
var level = Random.Shared.NextInt64(0, 6);
21-
Log.Logger.Write((LogEventLevel)level, "Message");
21+
Log.Logger.Write((LogEventLevel)level, "Message {counter}", logsCounter);
2222
await Task.Delay(500);
2323

2424
logsCounter++;

examples/WebApp/Program.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
using Serilog;
55
using Serilog.Events;
66
using System.Collections.Immutable;
7-
using X.Serilog.Sinks.Telegram;
87
using X.Serilog.Sinks.Telegram.Batch.Rules;
98
using X.Serilog.Sinks.Telegram.Configuration;
109
using X.Serilog.Sinks.Telegram.Extensions;
11-
using X.Serilog.Sinks.Telegram.Filters;
1210

1311
var builder = WebApplication.CreateBuilder(args);
1412

@@ -29,12 +27,6 @@
2927
config.LogFiltersConfiguration = new LogsFiltersConfiguration
3028
{
3129
ApplyLogFilters = false,
32-
FiltersOperator = LogFiltersOperator.And,
33-
Filters = new IFilter[]
34-
{
35-
new LogMessageContainsFilter("absolutely required term"),
36-
new LogMessageNotContainsFilter("term to skip")
37-
}.ToImmutableList()
3830
};
3931
config.FormatterConfiguration = new FormatterConfiguration
4032
{
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Immutable;
2-
using X.Serilog.Sinks.Telegram.Filters;
1+
using X.Serilog.Sinks.Telegram.Filters.Fluent;
32

43
namespace X.Serilog.Sinks.Telegram.Configuration;
54

@@ -14,12 +13,7 @@ public class LogsFiltersConfiguration
1413
public bool ApplyLogFilters { get; init; }
1514

1615
/// <summary>
17-
/// Gets or initializes the operator used for combining filters. The default is 'And'.
16+
/// Logs filtering query builder
1817
/// </summary>
19-
public LogFiltersOperator FiltersOperator { get; init; } = LogFiltersOperator.And;
20-
21-
/// <summary>
22-
/// Gets or initializes the list of filters to be applied.
23-
/// </summary>
24-
public IImmutableList<IFilter>? Filters { get; init; }
18+
public ILogQueryBuilder? QueryBuilder { get; set; }
2519
}

src/X.Serilog.Sinks.Telegram/Extensions/DependencyInjectionExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Immutable;
22
using X.Serilog.Sinks.Telegram.Batch.Rules;
33
using X.Serilog.Sinks.Telegram.Configuration;
4-
using X.Serilog.Sinks.Telegram.Filters;
4+
using X.Serilog.Sinks.Telegram.Filters.Fluent;
55

66
namespace X.Serilog.Sinks.Telegram.Extensions;
77

@@ -48,8 +48,7 @@ private static LoggerConfiguration TelegramCoreInternal(
4848
config.FormatterConfiguration = FormatterConfiguration.Default;
4949
config.LogFiltersConfiguration = new LogsFiltersConfiguration
5050
{
51-
ApplyLogFilters = false,
52-
Filters = new ImmutableArray<IFilter>()
51+
ApplyLogFilters = false
5352
};
5453
},
5554
messageFormatter: null!,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public class Condition(LogEntryPredicate predicate)
4+
{
5+
public LogEntryPredicate Predicate { get; set; } = predicate;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public class ConditionGroup(bool isOrGroup = false)
4+
{
5+
public List<object> Conditions { get; set; } = [];
6+
public bool IsOrGroup { get; set; } = isOrGroup;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public interface IExceptionRuleBuilder : ILogQueryBuilder
4+
{
5+
ILogQueryBuilder Contains(string substring);
6+
ILogQueryBuilder NotContains(string substring);
7+
ILogQueryBuilder Equals(string message, StringComparison comparison);
8+
ILogQueryBuilder NotEquals(string message, StringComparison comparison);
9+
ILogQueryBuilder Null();
10+
ILogQueryBuilder NotNull();
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public interface IFilterExecutor
4+
{
5+
bool Evaluate(LogEvent entry);
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public interface ILevelRuleBuilder : ILogQueryBuilder
4+
{
5+
ILogQueryBuilder InRange(LogEventLevel min, LogEventLevel max);
6+
ILogQueryBuilder NotInRange(LogEventLevel min, LogEventLevel max);
7+
ILogQueryBuilder Equals(LogEventLevel level);
8+
ILogQueryBuilder NotEquals(LogEventLevel level);
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace X.Serilog.Sinks.Telegram.Filters.Fluent;
2+
3+
public interface ILogQueryBuilder
4+
{
5+
IMessageRuleBuilder Message { get; }
6+
IExceptionRuleBuilder Exception { get; }
7+
IPropertiesRuleBuilder Properties { get; }
8+
ILevelRuleBuilder Level { get; }
9+
ILogQueryBuilder And();
10+
ILogQueryBuilder Or();
11+
}

0 commit comments

Comments
 (0)