PatternString ClassApache log4net™ SDK Documentation
This class implements a patterned string.
Inheritance Hierarchy

SystemObject
  log4net.UtilPatternString

Namespace: log4net.Util
Assembly: log4net (in log4net.dll) Version: 2.0.8.0-.NET 4.0
Syntax

public class PatternString : IOptionHandler

The PatternString type exposes the following members.

Constructors

  NameDescription
Public methodPatternString
Default constructor
Public methodPatternString(String)
Constructs a PatternString
Top
Properties

  NameDescription
Public propertyConversionPattern
Gets or sets the pattern formatting string
Top
Methods

  NameDescription
Public methodActivateOptions
Initialize object options
Public methodAddConverter(ConverterInfo)
Add a converter to this PatternString
Public methodAddConverter(String, Type)
Add a converter to this PatternString
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
(Inherited from Object.)
Public methodFormat
Format the pattern as a string
Public methodFormat(TextWriter)
Produces a formatted string as specified by the conversion pattern.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a String that represents the current Object.
(Inherited from Object.)
Top
Remarks

This string has embedded patterns that are resolved and expanded when the string is formatted.

This class functions similarly to the PatternLayout in that it accepts a pattern and renders it to a string. Unlike the PatternLayout however the PatternString does not render the properties of a specific LoggingEvent but of the process in general.

The recognized conversion pattern names are:

Conversion Pattern NameEffect
appdomain

Used to output the friendly name of the current AppDomain.

appsetting

Used to output the value of a specific appSetting key in the application configuration file.

date

Used to output the current date and time in the local time zone. To output the date in universal time use the %utcdate pattern. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %date{HH:mm:ss,fff} or %date{dd MMM yyyy HH:mm:ss,fff}. If no date format specifier is given then ISO8601 format is assumed (Iso8601DateFormatter).

The date format specifier admits the same syntax as the time pattern string of the ToString(string).

For better results it is recommended to use the log4net date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormatter, DateTimeDateFormatter and respectively Iso8601DateFormatter. For example, %date{ISO8601} or %date{ABSOLUTE}.

These dedicated date formatters perform significantly better than ToString(string).

env

Used to output the a specific environment variable. The key to lookup must be specified within braces and directly following the pattern specifier, e.g. %env{COMPUTERNAME} would include the value of the COMPUTERNAME environment variable.

The env pattern is not supported on the .NET Compact Framework.

identity

Used to output the user name for the currently active user (Principal.Identity.Name).

newline

Outputs the platform dependent line separator character or characters.

This conversion pattern name offers the same performance as using non-portable line separator strings such as "\n", or "\r\n". Thus, it is the preferred way of specifying a line separator.

processid

Used to output the system process ID for the current process.

property

Used to output a specific context property. The key to lookup must be specified within braces and directly following the pattern specifier, e.g. %property{user} would include the value from the property that is keyed by the string 'user'. Each property value that is to be included in the log must be specified separately. Properties are stored in logging contexts. By default the log4net:HostName property is set to the name of machine on which the event was originally logged.

If no key is specified, e.g. %property then all the keys and their values are printed in a comma separated list.

The properties of an event are combined from a number of different contexts. These are listed below in the order in which they are searched.

the thread properties
The Properties that are set on the current thread. These properties are shared by all events logged on this thread.
the global properties
The Properties that are set globally. These properties are shared by all the threads in the AppDomain.
random

Used to output a random string of characters. The string is made up of uppercase letters and numbers. By default the string is 4 characters long. The length of the string can be specified within braces directly following the pattern specifier, e.g. %random{8} would output an 8 character string.

username

Used to output the WindowsIdentity for the currently active user.

utcdate

Used to output the date of the logging event in universal time. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %utcdate{HH:mm:ss,fff} or %utcdate{dd MMM yyyy HH:mm:ss,fff}. If no date format specifier is given then ISO8601 format is assumed (Iso8601DateFormatter).

The date format specifier admits the same syntax as the time pattern string of the ToString(string).

For better results it is recommended to use the log4net date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormatter, DateTimeDateFormatter and respectively Iso8601DateFormatter. For example, %utcdate{ISO8601} or %utcdate{ABSOLUTE}.

These dedicated date formatters perform significantly better than ToString(string).

%

The sequence %% outputs a single percent sign.

Additional pattern converters may be registered with a specific PatternString instance using [M:AddConverter(ConverterInfo)] or [M:AddConverter(string, Type)].

See the PatternLayout for details on the format modifiers supported by the patterns.

See Also

Reference