Once it has been determined that the LoggingEvent should be written using the Appender, the message is passed to the Layout object. Table 8.5 on p. 206 lists the currently available Layout classes.
Type | Description |
|---|---|
log4net.Layout.SimpleLayout | Formats the logging event very simply: [Level] - [message] |
log4net.Layout.XMLLayout | Formats the logging event as an XML element. |
log4net.Layout.PatternLayout | Formats the logging event according to a flexible set of formatting flags. |
If an Appender utilizes a layout, the Simple Layout will be the default Layout for an Appender. The XMLLayout is a very useful Layout, as seen in Listing 8.6.
<log4net:event logger="IPWindowsApp.Form1" timestamp="2003-10-26T23:34:01.4237872-06:00"level="DEBUG" thread="3348" domain="ProcWindowsApp.exe" username="BNANTZ-XP\bnantz"> <log4net:message>This is just a test of using WMI with Log4Net.</log4net:message> <log4net:mdc> <log4net:data name="clock" value="1993 MHz" /> <log4net:data name="mem" value="536199168 bytes" /> </log4net:mdc> </log4net:event>
Probably the most flexible Layout is the Pattern Layout. The Pattern Layout allows you to specify a ConversionPattern string to transform the LoggingEvent into a more useful message. Table 8.6 shows the variables you can use to form a ConversionString.
Listing 8.7 demonstrates how to build up a PatternLayout. Cross-referencing Table 8.6, this pattern outputs date, thread name, priority, category, ndc, message, and newline.
%d [%t] %-5p %c [%x] - %m%n
In addition to the variables defined by Log4Net, there is also formatting of the variables shown in Listing 8.7 as -5. You can use the minus sign (-) to left-justify within a field. The default is to right-justify (pad on left) a field. A positive integer can be used to specify the minimum field width. If the data item requires fewer characters, it is padded with space(s) on either the left or the right until the minimum width is reached. If the item is larger than the minimum field width, the field is expanded to accommodate the data returned from the variable. You can also specify the maximum field width by using a period followed by a positive integer. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the item rather than the end.
You can also create your own pattern variables in your code for use by the PatternLayout's ConfigurationString.
These Layouts are useful for logging string messages, but if you want to log the state of an Object, then you need to use an ObjectRenderer.