Layouts 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.Table 8.5. Log4Net LayoutsType | 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.Listing 8.6. Sample Output Using XMLLayout <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.Table 8.6. Pattern Layout Format StringsVariable | Description |
---|
%m | Outputs your message. | %p | Outputs the priority of the logging event. | %r | Outputs the number of milliseconds elapsed since the start of the application until the creation of the logging event. | %c | Outputs the category of the logging event. Example: For the category name "a.b.c," the pattern %c{2} will output "b.c." {2} means "output last two components of the dot-separated category name." If no {n} is there, the full category name is output by default. | %t | Outputs the name of the thread that generated the logging event. | %x | Outputs the nested diagnostic context (NDC) associated with the thread that generated the logging event. Useful when multiple clients are handled by separate threads at a server process. | %X{} | Outputs the message diagnostic context (MDC) associated with the thread that generated the logging event. Useful when multiple clients are handled by separate threads at a server process. | %n | Outputs the platform-dependent newline character(s). Preferable to specifying "\n" or "\r\n" etc. | %% | Outputs a single percent sign. | %d | Outputs the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. Example: %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS} . If no date format specifier is given, then ISO8601 format is assumed. The date format specifier admits the same syntax as the time pattern string of Java's SimpleDateFormat, which is slow. For faster performance, use %d{ISO8601} , %d{ABSOLUTE} , %d{RELATIVE} (millisecs since program start, fastest) or %d{DATE} , which use log4j's ISO8601DateFormat, AbsoluteTimeDateFormat, RelativeTimeDateFormat, and DateTimeDateFormat date formatters, respectively. | %C | Outputs the fully qualified class name of the caller issuing the logging request. Example: For the class name "org.apache.xyz.SomeClass," the pattern %C{1} will output "SomeClass." {1} means "output last one component of the fully-qualified class name." If no {n} is there, the full class name is output by default. | %M | Outputs the method name where the logging request was issued. | %F | Outputs the file name where the logging request was issued. | %L | Outputs the line number from where the logging request was issued. | %l | Outputs source code location information. Shortcut for %C.%M(%F:%L). | 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.Listing 8.7. PatternLayout %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. |