# Filtering events

**Prerequisites**:

* Install [abstractions module](/logging/modules/abstractions.md)
* Read about general log [configuration practices](/logging/configuration.md)
* Read about [log interface](/logging/concepts/log-interface.md)
* Read about [log events](/logging/concepts/log-events.md)

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

```csharp
log = log.WithMinimumLevel(LogLevel.Warn);
```

Selectively disable some log levels:

```csharp
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:

```csharp
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:

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

### Filter by all properties

Select events whose properties dictionary satisfies given predicate:

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

Drop events whose properties dictionary satisfies given predicate:

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

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

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

```csharp
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:

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

### Filter by arbitrary event content

Select events satisfying given predicate:

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

Drop events satisfying given predicate:

```csharp
log.DropEvents(@event => @event.Exception == null);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vostok.gitbook.io/logging/how-to-guides/filtering-events-by-level.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
