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