Filtering events
Prerequisites:
Filtering allows a particular ILog instance to only record a subset of incoming log events. Events can be filtered by level, properties or literally any other information present.

Filter by level

Set a minimum allowed log level (events of lower levels will be dropped):
1
log = log.WithMinimumLevel(LogLevel.Warn);
Copied!
Selectively disable some log levels:
1
log = log.WithDisabledLevels(LogLevel.Debug, LogLevel.Fatal);
Copied!

Filter by a single property

Select events having a property with given name and a of given type value satisfying a predicate:
1
log = log.WithEventsSelectedByProperty<string>("prop", value => value == "foo");
Copied!
Drop events having a property with given name and a value of given type satisfying a predicate:
1
log = log.WithEventsDroppedByProperty<string>("prop", value => value == "foo");
Copied!

Filter by all properties

Select events whose properties dictionary satisfies given predicate:
1
log = log.WithEventsSelectedByProperties(props => props.ContainsKey("prop"));
Copied!
Drop events whose properties dictionary satisfies given predicate:
1
log = log.WithEventsDroppedByProperties(props => props.ContainsKey("prop"));
Copied!

Filter by source context

Select events having a source context with given value (values are case-insensitive prefixes):
1
log = log.WithEventsSelectedBySourceContext("foo");
2
3
log.ForContext("foo").Info("Hello!"); // will be logged
4
log.ForContext("bar").Info("Hello!"); // will not be logged
Copied!
Drop events having a source context with given value (values are case-insensitive prefixes):
1
log = log.WithEventsDroppedBySourceContext("foo");
2
3
log.ForContext("foo").Info("Hello!"); // will not be logged
4
log.ForContext("bar").Info("Hello!"); // will be logged
Copied!
Drop events below some level having a source context with given value (values are case-insensitive prefixes):
1
log = log.WithMinimumLevelForSourceContext("foo", LogLevel.Warn);
2
3
log.ForContext("foo").Info("Hello!"); // will not be logged
4
log.ForContext("foo").Warn("Hello!"); // will be logged
Copied!
There's also an alternative syntax that allows to infer context values from type names:
1
log = log.WithEventsSelectedBySourceContext<FooClass>();
2
log = log.WithEventsDroppedBySourceContext<FooClass>();
3
log = log.WithMinimumLevelForSourceContext<FooClass>(LogLevel.Warn);
Copied!

Filter by arbitrary event content

Select events satisfying given predicate:
1
log = log.SelectEvents(@event => @event.Exception == null);
Copied!
Drop events satisfying given predicate:
1
log.DropEvents(@event => @event.Exception == null);
Copied!