Filters Filters are associated with an Appender. A Filter allows you to set even stricter requirements on your LoggingEvent message. A number of predefined Filters are available in Log4Net. The DenyAll Filter allows you to deny all LoggingEvents unless specifically granted access. This is the exact opposite of the default behavior, but obviously it can be useful. The LevelMatch and LevelRange Filters are pretty self-explanatory for Filtering based on a Level. The StringMatch Filter allows you to filter a Logging Event based on a regular expression query. The NDC and MDC Filters are also similar string-based Filters. All of these Filters inherit from FilterSkeleton, which in turn implements IFilter. This makes creating your own custom Filter fairly straightforward, as shown later in the chapter under "Custom Filters."When a LoggingEvent is passed through a Filter, one of three values is returned: ACCEPT, DENY, or NEUTRAL.The real power of filtering comes when you link several Filters together. This is referred to as Filter Chaining. If DENY is returned from a Filter, the LoggingEvent is "dropped" for that Appender, and there is no need for the message to be passed on down the chain. If ACCEPT is returned, the LoggingEvent is logged, and the chain is broken. If NEUTRAL is returned from the Filter, the LoggingEvent is passed on to the next Filter in the chain, so forth and so on, down through all the Filters. If the LoggingEvent passes through the Filter Chain without an explicit ACCEPT or DENY, then it is implicitly accepted. The DenyAll Filter can be placed at the end of the Filter chain to reverse this implicit acceptance behavior.While Filters allow flexibility in what to log to the Appender, Layouts tell the Appender how the logged message should look. |