Vostok.Logging
HomeQuickstartModulesImplementations
1.0.0
1.0.0
  • Home
  • Quickstart
  • Guarantees
  • Configuration
  • Concepts and basics
    • Log interface
    • Log events
    • Syntax
      • Logging extensions
      • Message templates
      • Providing property values
    • Formatting
      • Output templates
      • Special properties
      • Format specifiers
    • Source context
    • Operation context
  • Modules
    • Abstractions
    • Configuration
    • Formatting
    • Console
    • File
    • Hercules
    • Context
    • Serilog
    • Log4net
    • NUnit
    • Microsoft
  • Implementations
    • Silent log
    • Console log
    • File log
    • Hercules log
  • Integrations
    • Serilog integration
    • Log4net integration
    • Microsoft logging integration
  • How-to guides
    • Using operation context
    • Using static log provider
    • Filtering events
    • Enriching events
    • Transforming events
    • Combining multiple logs
    • Custom output templates
    • External configuration rules
Powered by GitBook
On this page
  • Filter by level
  • Filter by a single property
  • Filter by all properties
  • Filter by source context
  • Filter by arbitrary event content
  1. How-to guides

Filtering events

Prerequisites:

  • Install abstractions module

  • Read about general log configuration practices

  • Read about log interface

  • Read about log events

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):

log = log.WithMinimumLevel(LogLevel.Warn);

Selectively disable some log levels:

log = log.WithDisabledLevels(LogLevel.Debug, LogLevel.Fatal);

Filter by a single property

Select events having a property with given name and a of given type value satisfying a predicate:

log = log.WithEventsSelectedByProperty<string>("prop", value => value == "foo");

Drop events having a property with given name and a value of given type satisfying a predicate:

log = log.WithEventsDroppedByProperty<string>("prop", value => value == "foo");

Filter by all properties

Select events whose properties dictionary satisfies given predicate:

log = log.WithEventsSelectedByProperties(props => props.ContainsKey("prop"));

Drop events whose properties dictionary satisfies given predicate:

log = log.WithEventsDroppedByProperties(props => props.ContainsKey("prop"));

Filter by source context

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:

log = log.WithEventsSelectedBySourceContext<FooClass>();
log = log.WithEventsDroppedBySourceContext<FooClass>();
log = log.WithMinimumLevelForSourceContext<FooClass>(LogLevel.Warn);

Filter by arbitrary event content

Select events satisfying given predicate:

log = log.SelectEvents(@event => @event.Exception == null);

Drop events satisfying given predicate:

log.DropEvents(@event => @event.Exception == null);
PreviousUsing static log providerNextEnriching events

Last updated 5 years ago