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 Layout RendererWhy would I want to write a Layout Renderer?Layout renderers are a way to enrich your debugging output by outputting environmental properties of your applications. NLog comes with Layout Renderers for most common tasks (like outputting environment variables, registry keys, thread ids, base directory, etc.), but you may want to develop your own Layout Renderer to provide information about your application or framework. How to write a layout renderer?
It's really easy. Create a class that inherits from ExampleThis is a skeleton layout renderer that outputs current hour. Compile using:
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 written to the console to be prepended with current hour. Simple isn't it? How to pass configuration options to the renderer?Consider the above example. There's a property called "ShowMinutes" that does just that. Having a public property that sets the required configuration parameters is enough for NLog to use it. Parameters can be passed by separating them with colons. For example: ${hour:showminutes=true}sets the ShowMinutes property to True during configuration. To pass more parameters, just use more colons: ${layoutrenderername:par1=value1:par2=value2:par3=value3:...:parN=valueN}NLog takes care of the appropriate conversions necessary so that you can use integer, string, datetime, boolean parameters. Do I really need to create a separate DLL?Not really. You can use LayoutRendererFactory.AddLayoutRenderer() 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. |