Termite Logging Framework for .NET termite VS Tool online logging
tweet tweet tweet tweet tweet

Filtering Log Entries

Filtering log entries is the process of ignoring some entries and actually logging others. Basic filtering is done by setting the severity threshold of a logger.

logger.SeverityThreshold = LogSeverity.Warning;

In the above example, all log requests that are of a severity lower than Warning will be ignored. The default severity threshold for loggers is Debug, which is the lowest severity.

You may wish to have more sophisticated filtering, however. For example, one logger might be specifically for database related log entries, whereas another might be for recording the general state of the application. You can use the provided LogEntryCategoryFilter class to accomplish this filtering. The following example shows how you might use it.

/* create and setup the filter */
LogEntryCategoryFilter myDbFilter = new LogEntryCategoryFilter(true);

/* now set the logger's filter */
logger.Filter = myDbFilter;

Using the logger above, only entries with the "Database" category will get logged. For example:

logger.LogInfo( "This has no category, and won't get logged" );
logger.LogInfo( "Network", "This won't get logged either" );
logger.LogInfo( "Database", "This will get logged" );

You can easily create a CompositeLogger which contains other loggers, each with its own filter.

Also, you can create your own custom filter by simply sublassing LogEntryFilter, overriding the method CanPass(LogEntry).

By default, if you don't specify a filter for a logger, it will use a LogEntryPassFilter, which allows all log entries to get logged. You can set your own default filter class by doing something such as:

Logger.DefaultFilterClass = typeof( MyCustomLogEntryFilter );