Features

Overview

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary. The log4net package is designed so that log statements can remain in shipped code without incurring a high performance cost. It follows that the speed of logging (or rather not logging) is crucial.

At the same time, log output can be so voluminous that it quickly becomes overwhelming. One of the distinctive features of log4net is the notion of hierarchical loggers. Using these loggers it is possible to selectively control which log statements are output at arbitrary granularity.

log4net is designed with two distinct goals in mind: speed and flexibility

What does log4net offer?

Ready-to-Use

log4net includes a wide range of built-in modules for common and advanced use cases.

  • Appenders targeting files, network sockets, databases, SMTP servers, etc.

  • Layouts that can render Text, XML, JSON, Syslog, etc. formatted outputs

  • Filters based on log levels, regular expressions, etc.

Reliability

log4net is built with solid reliability in mind. It can automatically reload its configuration upon modification and will do so without losing log events while reconfiguration occurs.

Performance

When configured correctly, log4net can deliver excelling performance without sacrificing reliability.

Support for multiple frameworks

log4net has specific builds for the following frameworks:

  • .NET Core 8 or higher - using netstandard-2.0

  • Microsoft .NET Framework 4.6.2 or higher

Output to multiple logging targets

log4net ships with the following appenders

Type Description

AdoNetAppender

Writes logging events to a database using either prepared statements or stored procedures.

AnsiColorTerminalAppender

Writes color highlighted logging events to a an ANSI terminal window.

AspNetTraceAppender

Writes logging events to the ASP trace context. These can then be rendered at the end of the ASP page or on the ASP trace page.

BufferingForwardingAppender

Buffers events and then forwards them to attached appenders.

ConsoleAppender

Writes color highlighted logging events to the application’s Windows Console.

ConsoleAppender

Writes logging events to the application’s Console. The events may go to either the standard our stream or the standard error stream.

DebugAppender

Writes logging events to the .net debugger (System.Diagnostics.Debug).

EventLogAppender

Writes logging events to the Windows Event Log.

FileAppender

Writes logging events to a file in the file system.

LocalSyslogAppender

Writes logging events to the local syslog service (UNIX only).

ManagedColoredConsoleAppender

Writes color highlighted logging events to the application’s Windows Console. This appender is a managed version of the ColoredConsoleAppender and does not require the use of the Windows Console API. It is a replacement for the ColoredConsoleAppender and is the recommended appender for logging to the Console.

MemoryAppender

Stores logging events in an in memory buffer.

OutputDebugStringAppender

Writes logging events to the debugger (using OutputDebugString). If the application has no debugger, the system debugger displays the string. If the application has no debugger and the system debugger is not active, the message is ignored.

RemoteSyslogAppender

Writes logging events to a remote syslog service using UDP networking.

RollingFileAppender

Writes logging events to a file in the file system. The RollingFileAppender can be configured to log to multiple files based upon date or file size constraints.

SmtpAppender

Sends logging events to an email address.

SmtpPickupDirAppender

Sends logging events to an email address but writes the emails to a configurable directory rather than sending them directly via SMTP.

TelnetAppender

Clients connect via Telnet to receive logging events.

TraceAppender

Writes logging events to the .NET trace system (System.Diagnostics.Trace).

UdpAppender

Sends logging events as connectionless UDP datagrams to a remote host or a multicast group using a UdpClient.

ForwardingAppender

Can be used to wrap another appender, for example to attach additional filters.

Hierarchical logging architecture

Hierarchical logging is an ideal fit with component based development. Each component has its own of logger. When individually tested, the properties of these loggers may be set as the developer requires. When combined with other components, the loggers inherit the properties determined by the integrator of the components. One can selectively elevate logging priorities on one component without affecting the other components. This is useful when you need a detailed trace from just a single component without crowding the trace file with messages from other components. All this can be done through configuration files - no code changes are required.

XML Configuration

log4net is configured using an XML configuration file. The configuration information can be embedded within other XML configuration files (such as the application’s .config file) or in a separate file. The configuration is easily readable and updateable while retaining the flexibility to express all configurations.

Alternatively log4net can be configured programmatically.

Dynamic Configuration

log4net can monitor its configuration file for changes and dynamically apply changes made by the configurator. The logging levels, appenders, layouts, and just about everything else can be adjusted at runtime. In many cases it is possible to diagnose application issues without terminating the process in question. This can a very valuable tool in investigating issues with deployed applications.