国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

log4net.Layout.PatternLayout用conversion模式格式化日志事件【翻譯】

2019-11-14 15:57:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

原文地址

log4net.Layout.PatternLayout,是一個(gè)靈活的布局,配置模式字符串。

線程安全。該類(lèi)型的 Public static 成員對(duì)多線程操作是安全的。實(shí)例成員不保證線程安全。

注意:

該類(lèi)的目標(biāo)是把 LoggingEvent 格式化成字符串。格式化的結(jié)果依賴于 conversion 模式。

conversion 模式與 C 語(yǔ)言的 PRintf 函數(shù)很像。conversion 模式是由文字文本和格式控制表達(dá)式(稱(chēng)為轉(zhuǎn)換指定符)組成。

你可以自由地將任何文字文本插入到轉(zhuǎn)換模式中。

每個(gè)轉(zhuǎn)換指定符(conversion specifier)以百分號(hào)(%)開(kāi)始,后面跟一個(gè)可選的格式修飾符(format modifiers)和轉(zhuǎn)換模式(conversion pattern)名稱(chēng)。 轉(zhuǎn)換模式名稱(chēng)指定數(shù)據(jù)的類(lèi)型,例如,日志器(logger)、級(jí)別(level)、日期(date)、線程名(thread name)。格式修飾符控制諸如,字段寬度,填充,左邊和右邊對(duì)齊等事情。

下面是一個(gè)簡(jiǎn)單的例子:

轉(zhuǎn)換模式為 "%-5level [%thread]: %message%newline",并假設(shè) log4net 環(huán)境設(shè)置為 PatternLayout,那么,下面語(yǔ)句:

[C#]
ILog log = LogManager.GetLogger(typeof(TestApp));
log.Debug("Message 1");
log.Warn("Message 2");  

將輸出,

DEBUG [main]: Message 1
WARN  [main]: Message 2  

需要注意的是,文字和轉(zhuǎn)換指示器之間沒(méi)有明確的分隔符。當(dāng)模式解析器讀取一個(gè)轉(zhuǎn)換字符時(shí),它知道何時(shí)已經(jīng)達(dá)到了一個(gè)轉(zhuǎn)換指示器的末尾。上面的例子,在轉(zhuǎn)換指示符 %-5level 含義是,日志事件的級(jí)別占 5 個(gè)字符的寬度,并且左邊對(duì)齊。

下表是可以識(shí)別的轉(zhuǎn)換模式名稱(chēng):

轉(zhuǎn)換模式名稱(chēng)

效果

a

等價(jià)于 appdomain

appdomain

用于輸出當(dāng)產(chǎn)生日志事件時(shí)的 AppDomain 名稱(chēng)。

aspnet-cache

指定 %aspnet-cache 時(shí)輸出所有 cache 項(xiàng);指定 %aspnet-cache{key} 時(shí)只輸出 key 指定的項(xiàng)。

該模式對(duì) Compact Framework or Client Profile 不可用。

aspnet-context

指定 %aspnet-context 時(shí)輸出所有 context 項(xiàng);指定 %aspnet-context 時(shí)只輸出 key 指定的項(xiàng)。

該模式對(duì) Compact Framework or Client Profile 不可用。

aspnet-request

指定 %aspnet-request 時(shí)輸出所有請(qǐng)求參數(shù)的項(xiàng);指定 %aspnet-request{key} 時(shí)只輸出 key 指定的項(xiàng)。

該模式對(duì) Compact Framework or Client Profile 不可用。

aspnet-session

指定 %aspnet-session 時(shí)輸出所有 session 項(xiàng);指定 %aspnet-session{key} 時(shí)只輸出 key 指定的項(xiàng)。

該模式對(duì) Compact Framework or Client Profile 不可用。

c

等價(jià)于 logger

C

等價(jià)于 type

class

等價(jià)于 type

d

等價(jià)于 date

date

以 local time zone 輸出日志事件的日期。若輸出 universal time 日期要使用 %utcdate 模式。日期格式也可以在模式后用大括號(hào)指定,例如,%date{HH:mm:ss,fff}%date{dd MMM yyyy HH:mm:ss,fff}。若沒(méi)有指定日期格式,則將采用 ISO8601(Iso8601DateFormatter)。日期格式指示器跟 ToString 方法個(gè)式化日期時(shí)采用時(shí)間模式具有相同的語(yǔ)法。

推薦使用 log4net 日期格式化器,能得到更好的輸出。通過(guò)指定字符串“ABSOLUTE”,“DATE”和“ISO8601”中的一個(gè),分別表示 AbsoluteTimeDateFormatter, DateTimeDateFormatter 和  Iso8601DateFormatter。例如,%date{ISO8601}%date{ABSOLUTE}

這寫(xiě)專(zhuān)用的日期格式化要比 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

等價(jià)于 file

file

輸出發(fā)生日志請(qǐng)求的文件名。

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

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

identity

輸出當(dāng)前活躍用戶的用戶名(Principal.Identity.Name)。

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

l

等價(jià)于 location

L

等價(jià)于 line

location

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

位置信息(location information)依賴 CLI 的實(shí)現(xiàn),但通常是由調(diào)用方法的完整限定名(fully qualified name)組成,后面跟調(diào)用者源文件名和行號(hào)。

位置信息很有用。然而,它的產(chǎn)生相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用。

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

level

輸出日志事件的級(jí)別。

line

輸出發(fā)生日志請(qǐng)求時(shí)的行號(hào)。

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

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

logger

輸出日志事件的日志器。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.

例如,若日志器名為 "a.b.c",模式為 %logger{2},將輸出 "b.c".

m

等價(jià)于 message

M

等價(jià)于 method

message

輸出與日志事件相關(guān)聯(lián)的應(yīng)用程序提供的信息,也就是你敲入的信息。

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

輸出發(fā)生日志請(qǐng)求時(shí)的方法名。

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

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

n

等價(jià)于 newline

newline

輸出換行。換行是平臺(tái)依賴的,各個(gè)平臺(tái)可能不同。

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

輸出與生產(chǎn)日志事件線程有關(guān)的 NDC(nested diagnostic context)。

p

等價(jià)于 level

P

等價(jià)于 property

properties

等價(jià)于 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

等價(jià)于 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

等價(jià)于 thread

timestamp

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

thread

輸出產(chǎn)生日志事件的線程名稱(chēng)。如果沒(méi)有可用的線程名稱(chēng),則使用數(shù)字。

type

Used to output the fully qualified type name of the caller issuing the logging request. This 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 class name will be printed. By default the class name is output in fully qualified form.

For example, for the class name "log4net.Layout.PatternLayout", the pattern %type{1} will output "PatternLayout".

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

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

u

等價(jià)于 identity

username

輸出當(dāng)前活躍用戶的 WindowsIdentity。

警告:產(chǎn)生調(diào)用位置信息相當(dāng)慢。除非執(zhí)行速度不是問(wèn)題,否則要避免使用它。

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

等價(jià)于 username

x

等價(jià)于 ndc

X

等價(jià)于 mdc

%

連續(xù)兩個(gè)百分號(hào) %% 會(huì)輸出一個(gè)百分號(hào)。

The single letter patterns are deprecated in favor of the longer more descriptive pattern names.

By default the relevant information is output as is. However, with the aid of format modifiers it is possible to change the minimum field width, the maximum field width and justification.

The optional format modifier is placed between the percent sign and the conversion pattern name.

The first optional format modifier is the left justification flag which is just the minus (-) character. Then comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated.

This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, it the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the printf function in C where truncation is done from the end.

Below are various format modifier examples for the logger conversion specifier.

格式修飾符

左邊對(duì)齊

最小寬度

最大寬度

備注

%20logger

false

20

無(wú)

若日志器的名稱(chēng)小于 20 個(gè)字符,則左邊補(bǔ)空格。

%-20logger

true

20

無(wú)

若日志器的名稱(chēng)小于 20 個(gè)字符,則右邊補(bǔ)空格。

%.30logger

NA

無(wú)

30

若日志器的名稱(chēng)大于 30 個(gè)字符,則截取。

%20.30logger

false

20

30

若日志器的名稱(chēng)小于 20 個(gè)字符,則左邊補(bǔ)空格。但是,若日志器名稱(chēng)的長(zhǎng)度大于 30 個(gè)字符,則截取。

%-20.30logger

true

20

30

若日志器的名稱(chēng)小于 20 個(gè)字符,則右邊補(bǔ)空格。但是,若日志器名稱(chēng)的長(zhǎng)度大于 30 個(gè)字符,則截取。

關(guān)于調(diào)用者位置信息(caller location information)
模式 %type、%file、%line、%method、%location、%class、%C、%F、%L、%l 和 %M 都會(huì)產(chǎn)生調(diào)用者位置信息。位置信息使用 System.Diagnostics.StackTrace 類(lèi)來(lái)產(chǎn)生調(diào)用堆棧。調(diào)用者信息是從這個(gè)堆棧提取的。

注意

.NET Compact Framework 1.0 不支持 System.Diagnostics.StackTrace 類(lèi),因此,調(diào)用者位置信息也是不可用的。

注意

The System.Diagnostics.StackTrace class has this to say about Release builds:

"StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects. StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization."

This means that in a Release build the caller information may be incomplete or may not exist at all! Therefore caller location information cannot be relied upon in a Release build.

Additional pattern converters may be registered with a specific PatternLayout instance using the AddConverter method.

This is a more detailed pattern.

%timestamp [%thread] %level %logger %ndc - %message%newline

A similar pattern except that the relative time is right padded if less than 6 digits, thread name is right padded if less than 15 characters and truncated if longer and the logger name is left padded if shorter than 30 characters and truncated if longer.

%-6timestamp [%15.15thread] %-5level %30.30logger %ndc - %message%newline

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 营山县| 贵南县| 乌拉特前旗| 五家渠市| 犍为县| 县级市| 蕲春县| 高邑县| 青铜峡市| 澄江县| 通道| 凤凰县| 阿城市| 筠连县| 嘉义县| 庆云县| 库伦旗| 双牌县| 观塘区| 柳林县| 仙居县| 信宜市| 营口市| 江都市| 苏尼特右旗| 江津市| 武鸣县| 永定县| 河西区| 吴旗县| 罗定市| 商南县| 保德县| 香格里拉县| 弥勒县| 仁怀市| 江北区| 郎溪县| 沙雅县| 武冈市| 凤台县|