-
Notifications
You must be signed in to change notification settings - Fork 2
/
FilterByEventLogExtensions.cs
62 lines (54 loc) · 2.15 KB
/
FilterByEventLogExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using JetBrains.Annotations;
namespace Vostok.Logging.Abstractions
{
[PublicAPI]
public static class FilterByEventLogExtensions
{
/// <summary>
/// <para>Returns a wrapper log that only logs events matched by <paramref name="allow"/> predicate.</para>
/// </summary>
[Pure]
public static ILog SelectEvents([NotNull] this ILog log, [NotNull] Predicate<LogEvent> allow)
{
return new EventFilterLog(log, allow, true);
}
/// <summary>
/// <para>Returns a wrapper log that drops events matched by <paramref name="reject"/> predicate.</para>
/// </summary>
[Pure]
public static ILog DropEvents([NotNull] this ILog log, [NotNull] Predicate<LogEvent> reject)
{
return new EventFilterLog(log, reject, false);
}
private class EventFilterLog : ILog
{
private readonly ILog baseLog;
private readonly Predicate<LogEvent> criterion;
private readonly bool criterionAllowsEvents;
public EventFilterLog(ILog baseLog, Predicate<LogEvent> criterion, bool criterionAllowsEvents)
{
this.baseLog = baseLog ?? throw new ArgumentNullException(nameof(baseLog));
this.criterion = criterion ?? throw new ArgumentNullException(nameof(criterion));
this.criterionAllowsEvents = criterionAllowsEvents;
}
public void Log(LogEvent @event)
{
var criterionMatches = criterion(@event);
if (criterionMatches == criterionAllowsEvents)
{
baseLog.Log(@event);
}
}
public bool IsEnabledFor(LogLevel level)
{
return baseLog.IsEnabledFor(level);
}
public ILog ForContext(string context)
{
var baseLogForContext = baseLog.ForContext(context);
return ReferenceEquals(baseLogForContext, baseLog) ? this : new EventFilterLog(baseLogForContext, criterion, criterionAllowsEvents);
}
}
}
}