| | | 1 | | using Elsa.Diagnostics.StructuredLogs.Models; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Diagnostics.StructuredLogs.Services; |
| | | 4 | | |
| | | 5 | | public static class StructuredLogFilterEvaluator |
| | | 6 | | { |
| | | 7 | | public static bool Matches(StructuredLogEvent logEvent, StructuredLogFilter filter) |
| | | 8 | | { |
| | 19 | 9 | | if (filter.MinimumLevel is { } minimumLevel && logEvent.Level < minimumLevel) |
| | 0 | 10 | | return false; |
| | | 11 | | |
| | 19 | 12 | | if (filter.Levels?.Count > 0 && !filter.Levels.Contains(logEvent.Level)) |
| | 0 | 13 | | return false; |
| | | 14 | | |
| | 19 | 15 | | if (!StartsWith(logEvent.Category, filter.CategoryPrefix)) |
| | 1 | 16 | | return false; |
| | | 17 | | |
| | 18 | 18 | | if (!ContainsText(logEvent, filter.Text)) |
| | 0 | 19 | | return false; |
| | | 20 | | |
| | 18 | 21 | | if (!EqualsFilter(logEvent.TenantId, filter.TenantId)) |
| | 0 | 22 | | return false; |
| | | 23 | | |
| | 18 | 24 | | if (!EqualsFilter(logEvent.WorkflowDefinitionId, filter.WorkflowDefinitionId)) |
| | 0 | 25 | | return false; |
| | | 26 | | |
| | 18 | 27 | | if (!EqualsFilter(logEvent.WorkflowInstanceId, filter.WorkflowInstanceId)) |
| | 0 | 28 | | return false; |
| | | 29 | | |
| | 18 | 30 | | if (!EqualsFilter(logEvent.TraceId, filter.TraceId)) |
| | 0 | 31 | | return false; |
| | | 32 | | |
| | 18 | 33 | | if (!EqualsFilter(logEvent.SpanId, filter.SpanId)) |
| | 0 | 34 | | return false; |
| | | 35 | | |
| | 18 | 36 | | if (!EqualsFilter(logEvent.CorrelationId, filter.CorrelationId)) |
| | 0 | 37 | | return false; |
| | | 38 | | |
| | 18 | 39 | | if (!EqualsFilter(logEvent.SourceId, filter.SourceId)) |
| | 3 | 40 | | return false; |
| | | 41 | | |
| | 15 | 42 | | if (filter.From is { } from && logEvent.Timestamp < from) |
| | 0 | 43 | | return false; |
| | | 44 | | |
| | 15 | 45 | | if (filter.To is { } to && logEvent.Timestamp > to) |
| | 0 | 46 | | return false; |
| | | 47 | | |
| | 15 | 48 | | return true; |
| | | 49 | | } |
| | | 50 | | |
| | 126 | 51 | | private static bool EqualsFilter(string? value, string? filter) => string.IsNullOrWhiteSpace(filter) || string.Equal |
| | | 52 | | |
| | 19 | 53 | | private static bool StartsWith(string value, string? filter) => string.IsNullOrWhiteSpace(filter) || value.StartsWit |
| | | 54 | | |
| | 16 | 55 | | private static bool Contains(string? value, string? filter) => string.IsNullOrWhiteSpace(filter) || value?.Contains( |
| | | 56 | | |
| | | 57 | | private static bool ContainsText(StructuredLogEvent logEvent, string? filter) |
| | | 58 | | { |
| | 18 | 59 | | if (string.IsNullOrWhiteSpace(filter)) |
| | 14 | 60 | | return true; |
| | | 61 | | |
| | 4 | 62 | | return Contains(logEvent.Message, filter) |
| | 4 | 63 | | || Contains(logEvent.MessageTemplate, filter) |
| | 4 | 64 | | || Contains(logEvent.Category, filter) |
| | 4 | 65 | | || Contains(logEvent.Exception?.Message, filter) |
| | 4 | 66 | | || Contains(logEvent.Exception?.StackTrace, filter) |
| | 1 | 67 | | || logEvent.Properties.Any(x => Contains(x.Key, filter) || Contains(x.Value, filter)) |
| | 5 | 68 | | || logEvent.Scopes.Any(x => Contains(x.Key, filter) || Contains(x.Value, filter)); |
| | | 69 | | } |
| | | 70 | | } |