Last updated: 2006-09-18
This website is based on NLog v20060918.
Click here to view the documentation
for other versions.
| How to write your own FilterWhy would I want to write a Filter?Filters are a way to eliminate unnecessary logging messages and prevent them from reaching log targets. NLog comes with pre-defined filters that operate on string layouts and support most common operators. You need your own filter if none of the provided filters are suitable for your purpose. How to write a filter?
It's really easy. Create a class that inherits from
Typically it's up to the user to decide what to do with the message after the filter matches.
Each filter has a ExampleThis is an example filter that eliminates messages sent between the specified hours.
How to use the newly created layout renderer?It's easy. Just put the renderer in a DLL and reference it from the the config file using the <extensions /> clause as described here. Configuration file exampleThis example causes all messages between 10:00 and 12:59 to be ignored. Simple isn't it? How to pass configuration options to the filter?Consider the above example. There are properties called "FromHour" and "ToHour" that do just that. Having a public property that sets the required configuration parameters is enough for NLog to use it. Each attribute that you put in the filter definition gets passed to the appropriate public property. NLog takes care of the appropriate conversions necessary so that you can use integer, string, datetime, boolean parameters. The parameters are case-insensitive. <filters> ... <hourRange fromHour="10" toHour="12" action="Ignore" /> ... </filters>sets the FromHour property to 10 and ToHour to 12 during configuration. To pass more parameters, just use more attributes: <filters> ... <hourRange p1="" p2="" p3="" p4="" pN="" action="Ignore" /> ... </filters>Do I really need to create a separate DLL?Not really. You can use FilterFactory.AddFilter() to register your layout renderer programmatically. Just be sure to to it at the very beginning of your program before any log messages are written. It should be possible to reference your EXE using the <extensions /> clause. Last updated: 2006-07-10 11:32:55 | |||||||
| Copyright © 2004-2006 by Jarosław Kowalski. |