[log4net]PatternLayoutのConversionPatternで指定可能なパラメータ一覧

log4netでログを出力する際、layout type=”log4net.Layout.PatternLayout”の、ConversionPatternで指定可能なパラメータの種類と意味の一覧です。

元ネタはこちら> http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

パターン名 内容
a appdomainが指定されたのと同じ意味です。
appdomain AppDomainを、どこでログ出力されたかを分かりやすい形で出力します。
aspnet-cache ASP.NETでキャッシュされている内容を全て出力します。特定のものだけ出力したい場合は、%aspnet-cache{key}の形で指定します。このパラメータは、Compact Framework や、Client Profile assembliesでは使用できません。
aspnet-context ASP.NETの全コンテキストアイテム情報を出力します。特定のものだけ出力したい場合は、%aspnet-context{key}の形で指定します。
このパラメータは、Compact Framework や、Client Profile assembliesでは使用できません。
aspnet-request ASP.NETの全リクエストパラメータ情報を出力します。特定のものだけ出力したい場合は、%aspnet-context{key}の形で指定します。
このパラメータは、Compact Framework や、Client Profile assembliesでは使用できません。
aspnet-session ASP.NETの全セッション情報を出力します。特定のものだけ出力したい場合は、%aspnet-context{key}の形で指定します。
このパラメータは、Compact Framework や、Client Profile assembliesでは使用できません。
c loggerと同じ意味です。
       
C typeと同じ意味です。
       
class typeと同じ意味です。
       
d dateと同じ意味です。
       
date ログ出力が行われた時刻をlocal time zoneで出力します。UTCで出力したい場合は、%utcdateを使用してください。
日付の書式指定は、例えば”%date{HH:mm:ss,fff}”や、”%date{dd MMM yyyy HH:mm:ss,fff}”の形式で記述します。詳細はdate format specifierを参照してください。ISO8601で出力したい場合は、Iso8601DateFormatterを参照してください。日付指定には,
.NetFrameworkのDateTime.ToString()と同じ構文でいけますが、log4netの書式指定のほうが推奨されています。…
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.


exception

Used to output the exception passed in with the log message.

If an exception object is stored in the logging event it will be rendered into the pattern output with a trailing newline. If there is no exception then nothing will be output and no trailing newline will be appended. It is typical to put a newline before the exception and to have the exception as the last data in the pattern.

F fileを指定したときと同じです
file ログ出力のメソッド呼出元のコードがあるファイル名を出力します。
*注意*呼び出し元のファイル名を取得する処理は非常に遅いです。ですので、この指定をするのは速度が問題ないときに限って行ってください。呼び出し元の情報についてのavailabilityは後述の特記事項も参照してください。(訳注:翻訳前の元ページで確認してください)
identity

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

WARNING Generating caller information is extremely slow. Its use should be avoided unless execution speed is not an issue.

l locationと同じ意味です。
L lineと同じ意味です。
location

Used to output location information of the caller which generated the logging event.

The location information depends on the CLI implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses.

The location information can be very useful. However, its generation is extremely slow. Its use should be avoided unless execution speed is not an issue.

See the note below on the availability of caller location information.

level

Used to output the level of the logging event.

line

Used to output the line number from where the logging request was issued.

WARNING Generating caller location information is extremely slow. Its use should be avoided unless execution speed is not an issue.

See the note below on the availability of caller location information.

logger

Used to output the logger of the logging event. The logger conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

If a precision specifier is given, then only the corresponding number of right most components of the logger name will be printed. By default the logger name is printed in full.

For example, for the logger name “a.b.c” the pattern %logger{2} will output “b.c”.

m Equivalent to message
M Equivalent to method
message

Used to output the application supplied message associated with the logging event.

mdc

The MDC (old name for the ThreadContext.Properties) is now part of the combined event properties. This pattern is supported for compatibility but is equivalent to property.

method

Used to output the method name where the logging request was issued.

WARNING Generating caller location information is extremely slow. Its use should be avoided unless execution speed is not an issue.

See the note below on the availability of caller location information.

n Equivalent to newline
newline

Outputs the platform dependent line separator character or characters.

This conversion pattern 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.

ndc

Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.

p Equivalent to level
P Equivalent to property
properties Equivalent to property
property

Used to output the an event specific 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 added to events by loggers or appenders. 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 event properties
The event has Properties that can be set. These properties are specific to this event only.

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.

r Equivalent to timestamp
stacktrace

Used to output the stack trace of the logging event The stack trace level specifier may be enclosed between braces. For example, %stacktrace{level}. If no stack trace level specifier is given then 1 is assumed

Output uses the format: type3.MethodCall3 > type2.MethodCall2 > type1.MethodCall1

This pattern is not available for Compact Framework assemblies.

stacktracedetail

Used to output the stack trace of the logging event The stack trace level specifier may be enclosed between braces. For example, %stacktracedetail{level}. If no stack trace level specifier is given then 1 is assumed

Output uses the format: type3.MethodCall3(type param,…) > type2.MethodCall2(type param,…) > type1.MethodCall1(type param,…)

This pattern is not available for Compact Framework assemblies.

t Equivalent to thread
timestamp

Used to output the number of milliseconds elapsed since the start of the application until the creation of the logging event.

thread

Used to output the name of the thread that generated the logging event. Uses the thread number if no name is available.

type ログ出力メソッドの呼び出し元のクラス名をパッケージ名つきで出力します。このパラメータは後述する”精度指定”のオプションを付与する事もできます。パラメータは、波括弧に整数を指定します。
もし、精度指定が行われた時は、パッケージ名のうち指定された数値文が出力されます。未指定時は、パッケージ名を含めて全ての名称が出力されます。

例えば、ログ出力を行った処理のクラス名が“log4net.Layout.PatternLayout”だったときに,%type{1} を指定したら、“PatternLayout”がログ出力されます。

注意:呼び元のクラス情報を取得するのは処理速度的に遅くなってしまうので、このパラメータは速度が問題ないときに限って使用してください。

See the note below on the availability of caller location information.

u Equivalent to identity
username

Used to output the WindowsIdentity for the currently active user.

WARNING Generating caller WindowsIdentity information is extremely slow. Its use should be avoided unless execution speed is not an issue.

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.

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.

w Equivalent to username
x Equivalent to ndc
X Equivalent to mdc
% “%%”と指定すると、ログファイルに”%”を出力します。


ConversionPatternは、以下のような感じでapp.configに記述します。

<configuration>
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    ...
<configSections>
 
<log4net>
    <appender name="MyAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File"               value="log.txt"    /> 
        <param name="AppendToFile"       value="true"       />
        <param name="RollingStyle"       value="Date"       />
        <param name="DatePattern"        value="yyyy-MM-dd" />
        <param name="MaxSizeRollBackups" value="7"          /> 
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss} %-5p %20file(%4L) %m%n" />
        </layout>
    </appender>
 
    <root>
        <level value="ALL" />
        <appender-ref ref="MyAppender" />
    </root>
</log4net>





関連記事

コメントを残す

メールアドレスが公開されることはありません。