AppendersThe nicest feature of Log4Net is that you do not have to consider at design time how your log messages will be persisted. Log4Net abstracts that functionality by allowing you to log to a variety of destinations via an Appender. Because a Logger logs to an Appender interface, adding an Appender or changing an Appender at runtime is very easy! More importantly, logging code does not have to change to log to a different destination. Also, you do not have to implement the code to actually write the log to its destination. Table 8.3 is a list of available Appenders in Log4Net: Chapter 10, "Database Development," explores the ADO.NET provider for PostgreSQL, which would make an excellent log target. Unfortunately, Log4Net does not yet support a Fallback Appender, but I know it is on the to-do list. A fallback Appender allows you to specify a preferred Appender but also another Appender in case the preferred Appender fails. To add an Appender to a Logger, use the Logger method addAppender(Appender appender). As Figure 8-3 depicts, all Appenders inherit from AppenderSkeleton. Figure 8-3. Logger Inheritance.Therefore, all Appenders must have the following properties:
By using multiple Appenders and the threshold of the Appenders, you can direct log messages to different targets. So, as Listing 8.18 shows later in this chapter, you can configure Log4Net to log all messages to the eventlog and all FATAL messages to send email to a pager system. You may have noticed that Figure 8-3 alludes to BufferingAppenders. Some of the Appenders, like the SMTPAppender, may need to buffer up a specified number of messages before sending them. As was mentioned, Log4Net is platform-independent. Log4Net does support different CLI implementations in some capacity. This is not a flaw in Log4Net but is basically due to the lack of support in either the CLI implementation or the operating system platform itself. For instance, Log4Net does work on Windows CE, but the current version of the CLI implementation (.NET Compact Framework) does not support Remoting, so the Remoting Appender will not work on that platform. Table 8.4 lists the platforms currently supported by Log4Net.
The Appender is responsible for actually writing out the log messages. While you already have the ability to "Filter" messages based on the Logger's Level, you may want to "Filter" in a more configurable, larger capacity. Of course, Log4Net provides this functionality in the Filter class. |