Layouts
An appender uses a layout to encode a LogEvent into a form that meets the needs of whatever will be consuming the log event. This page will try to answer following questions:
Common Concerns
This section outlines some common concerns shared by most predefined layouts in log4net that users should be aware of.
Structured Logging
In modern production environments, logs are typically not read from local files by engineers. Instead, they are sent to log ingestion systems like Elasticsearch, Google Cloud Logging, or similar platforms for observability, metrics, and monitoring purposes.
To support this, logs must be encoded in a machine-readable format — a practice known as structured logging.
log4net provides support for structured logging through specialized layouts. For production use cases, we recommend using either:
-
XmlLayout
— for structured XML output. -
JsonLayout
— via the external project log4net.Ext.Json.
These layouts help ensure your logs are compatible with log aggregation and analysis tools.
Predefined Layouts
log4net includes a set of predefined layouts designed to support a variety of common logging scenarios.
PatternLayout
PatternLayout
is a customizable, efficient, and human-readable layout that generates log messages based on a user-defined pattern.
It works similarly to String.Format()
but provides specialized directives for injecting properties from a LoggingEvent
.
|
A conversion pattern is composed of literal text and conversion specifiers (format control expressions). For example, the following pattern:
logger.Debug("Woof!");
logger.Warn("Meow!");
will yield the output
2024-12-21 14:07:41,517 [main] DEBUG Animals.Carnivora.Dog - Woof!
2024-12-21 14:07:41,517 [main] WARN Animals.Carnivora.Dog - Meow!
DyamicPatternLayout
The DynamicPatternLayout
should be used whenever the header or footer needs to include information that may change over time.
Unlike the static PatternLayout
, which renders headers and footers only once, the DynamicPatternLayout
re-evaluates its pattern every time it is invoked.
This makes it possible, for example, to include the current date and time in the header or footer—something not possible with the static layout.
The following example shows how to configure the DynamicPatternLayout
:
<layout type="log4net.Layout.DynamicPatternLayout">
<header value="Log started at %date%newline" />
<footer value="Log ended at %date%newline" />
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
List of Layouts
Type | Description |
---|---|
|
Formats the logging event using a pattern string that is re-evaluated on every log event, allowing dynamic values like timestamps in headers or footers. |
|
Outputs only the exception text from the logging event. |
|
Formats the logging event using a flexible pattern string with various conversion specifiers. |
|
Outputs the raw timestamp (as a |
|
Outputs the raw timestamp in UTC from the logging event. |
|
Provides a very simple format: |
|
Formats the logging event as a basic XML element. |
|
Formats the logging event as XML compliant with the log4j event DTD. |