Skip to content

Built-in Filters

Filters decide whether a log message passes through. Each filter implements ILogFilter with a single method:

public interface ILogFilter
{
    bool ShouldLog(LogMessage logMessage);
}

Return true to allow the message, false to discard.

Two-tier filtering

Filters can be applied at two levels:

  1. Global -> added to LogContainer, applies to every sink
  2. Per-sink -> added to a CloudLogSink, applies only to that sink
// Global filter - affects all sinks
Log.Container.AddFilter(new LevelFilter(LogLevel.Warning));

// Per-sink filter - only affects this Discord sink
var discord = new DiscordSink("...");
discord.AddFilter(new RegexFilter("critical|error"));

All filter types

LevelFilter

Passes messages at or above a minimum level.

new LevelFilter(LogLevel.Warning)
// passes Warning, Error, Assert

LevelRangeFilter

Passes messages within an inclusive range.

new LevelRangeFilter(LogLevel.Info, LogLevel.Error)
// passes Info, Warning, Error

LevelSetFilter

Passes messages whose level is in the set.

new LevelSetFilter(new[] { LogLevel.Info, LogLevel.Error })
// only Info and Error pass

ClassFilter

Filters by class name. Extracts the class from the stack trace or source filename. Supports include and exclude modes.

// Pass only messages from Player or Enemy classes
new ClassFilter(new[] { "Player", "Enemy" }, minLevel: LogLevel.Debug, exclude: false);

// Exclude messages from InputHandler
new ClassFilter(new[] { "InputHandler" }, minLevel: LogLevel.Debug, exclude: true);

NamespaceFilter

Converts the source file directory path to dot notation and matches against a namespace. Supports recursive (sub-namespace) matching.

// Pass messages from MyGame.Gameplay namespace (and sub-namespaces)
new NamespaceFilter("MyGame.Gameplay", minLevel: LogLevel.Info, recursive: true);

DirectoryFilter

Matches the directory portion of the caller's source file path against a base path. Case-insensitive.

// Only allow messages from files in Scripts/AI/
new DirectoryFilter("Scripts/AI", includeSubdirs: true);

PathFilter

Custom predicate on the source file path.

new PathFilter(path => path != null && path.Contains("Plugins"));

RegexFilter

Matches the message text against a compiled regex pattern. Can be set to exclude matched messages.

// Only pass messages matching the pattern
new RegexFilter("error|fail|critical", RegexOptions.IgnoreCase, includeMatch: true);

// Drop messages matching the pattern
new RegexFilter("verbose|spam", RegexOptions.IgnoreCase, includeMatch: false);

RateLimitFilter

Limits messages within a sliding time window, keyed by message content (customizable).

// Max 5 messages per 10 seconds per unique message text
new RateLimitFilter(maxPerInterval: 5, interval: TimeSpan.FromSeconds(10));

// Custom key function - limit by level instead
new RateLimitFilter(5, TimeSpan.FromSeconds(10),
    keyFunc: msg => msg.Level.ToString());

// Modes: Drop (default) or Coalesce (reserved for future use)

TimeWindowFilter

Passes messages only during specified times of day and/or days of the week.

// Only log during business hours, weekdays
new TimeWindowFilter(
    start: TimeSpan.FromHours(9),
    end: TimeSpan.FromHours(17),
    days: new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday,
                  DayOfWeek.Thursday, DayOfWeek.Friday });

// Overnight range (start > end is handled automatically)
new TimeWindowFilter(
    start: TimeSpan.FromHours(22),
    end: TimeSpan.FromHours(6));

ChainedFilter

Combines multiple filters with AND logic (all must pass).

new ChainedFilter(new ILogFilter[] {
    new LevelFilter(LogLevel.Warning),
    new RegexFilter("error", RegexOptions.IgnoreCase)
});

This is used internally when you add multiple filters to LogContainer or a CloudLogSink.

CompositeFilter

Combines filters with AND or OR logic.

new CompositeFilter(CompositeMode.And, new ILogFilter[] {
    new LevelFilter(LogLevel.Warning),
    new NamespaceFilter("MyGame")
});

new CompositeFilter(CompositeMode.Or, new ILogFilter[] {
    new ClassFilter(new[] { "Player" }),
    new ClassFilter(new[] { "Enemy" })
});

SinkFilterHelper

When using LogixSettingsSO, the SinkFilterHelper.ApplyFilters() method automatically applies global and per-sink filters from FilterSettingsSO to each CloudLogSink:

SinkFilterHelper.ApplyFilters(sink, filterSettings);
// Adds: LevelSetFilter, NamespaceFilter, ClassFilter,
//       DirectoryFilter, RegexFilter

This is called automatically by LogContainer.LoadSettings() and LogixInitializer - no manual wiring needed.