Skip to content

Custom Filters

Implement ILogFilter to create your own filter:

using Logix.Abstractions;

public class KeywordFilter : ILogFilter
{
    private readonly string[] _keywords;

    public KeywordFilter(params string[] keywords) => _keywords = keywords;

    public bool ShouldLog(LogMessage msg)
    {
        if (msg.Message == null) return true;
        foreach (var kw in _keywords)
            if (msg.Message.ToString().Contains(kw, StringComparison.OrdinalIgnoreCase))
                return true;
        return false;
    }
}

ShouldLog receives the full LogMessage:

Field Type Description
Created DateTime Timestamp (local)
Level LogLevel Severity
Message object? The log message
File string? Caller source path
Line int Caller line number
StackTrace StackTrace? Call stack

Registration

Log.Container.AddFilter(new KeywordFilter("urgent", "critical"));

For per-sink filters on a CloudLogSink:

mySink.AddFilter(new KeywordFilter("urgent"));
mySink.RemoveFilter(filter);
mySink.ClearFilters();