This document presents example configurations for the built-in appenders. These configurations are designed to work with the log4net.Config.DOMConfigurator and the log4net.Repository.Hierarchy.Hierarchy.
These examples are by no means exhaustive configurations for the appenders. For a full list of the parameters that can be specified to each appender and more details on each options see the SDK documentation for the appender.
For full details see the SDK Reference entry: log4net.Appender.AdoNetAppender.
The configuration of the AdoNetAppender depends on the provider selected for the target database. Here are some examples.
The following example shows how to configure the AdoNetAppender to log messages to a SQL Server database. The events are written in batches of 100 (BufferSize). The ConnectionType specifies the fully qualified type name for the System.Data.IDbConnection to use to connect to the database. The ConnectionString is database provider specific. The CommandText is either a prepared statement or a stored procedure, in this case it is a prepared statement. Each parameter to the prepared statement or stored procedure is specified with its name, database type and a layout that renders the value for the parameter.
The database table definition is:
The appender configuration is:
This example shows how to write events to an Access Database.
This example shows how to write events to an Oracle9i Database.
The database table definition is:
The appender configuration is:
This example shows how to write events to an Oracle8i Database.
The database table definition is:
The appender configuration is:
This example shows how to write events to an IBM DB2 8.2 Database. The following syntax should also work with older DB2 database servers.
The database table definition is:
The appender configuration is:
This example shows how to write events to a SQLite Database. This was tested against v0.21 of the SQLite .NET provider.
SQLite doesn't have strongly-typed columns or field lengths but its recommended you still include this information for readability. The database table definition is:
The appender configuration is:
For full details see the SDK Reference entry: log4net.Appender.AspNetTraceAppender.
The following example shows how to configure the AspNetTraceAppender to log messages to the ASP.NET TraceContext. The messages are written to the System.Web.TraceContext.Write method if they are below level WARN. If they are WARN or above they are written to the System.Web.TraceContext.Warn method.
For full details see the SDK Reference entry: log4net.Appender.BufferingForwardingAppender.
The following example shows how to configure the BufferingForwardingAppender to buffer 100 messages before delivering them to the ConsoleAppender.
This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that the events will only be delivered when a message with level of WARN or higher level is logged. Up to 512 (BufferSize) previous messages of any level will also be delivered to provide context information. Messages not sent will be discarded.
For full details see the SDK Reference entry: log4net.Appender.ColoredConsoleAppender.
The following example shows how to configure the ColoredConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream. This example shows how to highlight error messages.
This example shows how to colorize multiple levels.
For full details see the SDK Reference entry: log4net.Appender.ConsoleAppender.
The following example shows how to configure the ConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream.
This example shows how to direct the log messages to the console error stream.
For full details see the SDK Reference entry: log4net.Appender.EventLogAppender.
The following example shows how to configure the EventLogAppender to log to the Application event log on the local machine using the event Source of the AppDomain.FriendlyName.
This example shows how to configure the EventLogAppender to use a specific event Source.
For more information on how to setup the event log to allow the EventLogAppender to write to it, see the FAQ: Why doesn't the EventLogAppender work?.
For full details see the SDK Reference entry: log4net.Appender.FileAppender.
The following example shows how to configure the FileAppender to write messages to a file. The file specified is log-file.txt. The file will be appended to rather than overwritten each time the logging process starts.
This example shows how to configure the file name to write to using an environment variable TMP. The encoding to use to write to the file is also specified.
This example shows how to configure the appender to use the minimal locking model that allows multiple processes to write to the same file.
This example shows how to configure the appender to use the "inter process" locking model.
Before you change the locking model so that multiple process may log to the same file, please read the FAQ.
For full details see the SDK Reference entry: log4net.Appender.ForwardingAppender.
The following example shows how to configure the ForwardingAppender. The forwarding appender allows a set of constraints to be used to decorate an appender. In this example the ConsoleAppender is decorated with a Threshold of level WARN. This means that an event directed to the ConsoleAppender directly will be logged regardless of its level, but an event directed to the ForwardingAppender will only be passed on to the ConsoleAppender if its level is WARN or higher. This appender is used only in special circumstances.
For full details see the SDK Reference entry: log4net.Appender.ManagedColoredConsoleAppender.
The following example shows how to configure the ManagedColoredConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream. This example shows how to highlight error messages.
This example shows how to colorize multiple levels.
For full details see the SDK Reference entry: log4net.Appender.MemoryAppender.
It is unlikely that the MemoryAppender will be configured using a config file, but if you want to do it here's how.
For full details see the SDK Reference entry: log4net.Appender.NetSendAppender.
The following example shows how to configure the NetSendAppender to deliver messages to a specific user's screen. As this appender is typically only used for important notifications a Threshold of level Error is specified. This example delivers the messages to the user nicko on the machine SQUARE. However things are not always straight forward using the Windows Messenger Service, one possible outcome using this configuration is that the Server will broadcast looking for a WINS server which it will then ask to deliver the message to the Recipient, the WINS server will deliver it to the first terminal that the user logged in from.
For full details see the SDK Reference entry: log4net.Appender.OutputDebugStringAppender.
The following example shows how to configure the OutputDebugStringAppender to write logging messages to the OutputDebugString API.
For full details see the SDK Reference entry: log4net.Appender.RemotingAppender.
The following example shows how to configure the RemotingAppender to deliver logging events to a specified Sink (in this example the sink is tcp://localhost:8085/LoggingSink). In this example the events are delivered in blocks of 95 events because of the BufferSize. No events are discarded. The OnlyFixPartialEventData option allows the appender to ignore certain logging event properties that can be very slow to generate (e.g. the calling location information).
This example configures the RemotingAppender to deliver the events only when an event with level ERROR or above is logged. When the events are delivered, up to 200 (BufferSize) previous events (regardless of level) will be delivered to provide context. Events not delivered will be discarded.
For full details see the SDK Reference entry: log4net.Appender.RollingFileAppender.
The RollingFileAppender builds on the FileAppender and has the same options as that appender.
The following example shows how to configure the RollingFileAppender to write to the file log.txt. The file written to will always be called log.txt because the StaticLogFileName param is specified. The file will be rolled based on a size constraint (RollingStyle). Up to 10 (MaxSizeRollBackups) old files of 100 KB each (MaximumFileSize) will be kept. These rolled files will be named: log.txt.1, log.txt.2, log.txt.3, etc...
This example show how to configure the RollingFileAppender to roll log files on a date period. This example will roll the log file every minute! To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every day. See System.Globalization.DateTimeFormatInfo for a list of available patterns.
This example show how to configure the RollingFileAppender to roll log files on a date period and within a date period on file size. For each day only the last 10 files of 1MB will be kept.
This example show how to configure the RollingFileAppender to roll log files once per program execution. The appendToFile property is set to false to prevent the appender from overwriting the existing files. The maxSizeRollBackups is set to negative 1 to allow an infinite number of backup files. The file size does have to be limited but here it is set to 50 Gigabytes which, if a log file exceeds this size limit during a single run then it will also be rolled.
For full details see the SDK Reference entry: log4net.Appender.SmtpAppender.
The following example shows how to configure the SmtpAppender to deliver log events via SMTP email. The To, From, Subject and SmtpHost are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged. Each email will also contain up to 512 (BufferSize) previous messages of any level to provide context. Messages not sent will be discarded.
This example shows how to configure the SmtpAppender to deliver all messages in emails with 512 (BufferSize) messages per email.
This example shows a more verbose formatting layout for the mail messages.
For full details see the SDK Reference entry: log4net.Appender.SmtpPickupDirAppender.
The SmtpPickupDirAppender is configured similarly to the SmtpAppender. The only difference is that rather than specify a SmtpHost parameter a PickupDir must be specified.
The PickupDir parameter is a path that must exist and the code executing the appender must have permission to create new files and write to them in this directory. The path is relative to the application's base directory (AppDomain.BaseDirectory).
The following example shows how to configure the SmtpPickupDirAppender to deliver log events via SMTP email. The To, From, Subject and PickupDir are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged. Each email will also contain up to 512 (BufferSize) previous messages of any level to provide context. Messages not sent will be discarded.
For full details see the SDK Reference entry: log4net.Appender.TraceAppender.
The following example shows how to configure the TraceAppender to log messages to the System.Diagnostics.Trace system. This is the tracing system supplied with the .net base class libraries. See the MSDN documentation for the System.Diagnostics.Trace class for more details on how to configure the trace system.
For full details see the SDK Reference entry: log4net.Appender.UdpAppender.
The following example shows how to configure the UdpAppender to send events to a RemoteAddress on the specified RemotePort.
For full details see the SDK Reference entry: log4net.Layout.DynamicPatternLayout.
The DynamicPatternLayout should be used whenever the header or footer should contain information that could change over time. Compared to the static PatternLayout which does not re-evaluate on every invoke, the DynamicPatternLayout does re-evaluate the pattern on every invoke. It does, for example, allow to include the current DateTime in the header and/or the footer which was not possible with the static PatternLayout.
The following example shows how to configure the DynamicPatternLayout.