亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 15:57:00
字體:
來源:轉載
供稿:網友

原文地址

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

線程安全。該類型的 Public static 成員對多線程操作是安全的。實例成員不保證線程安全。

注意:

該類的目標是把 LoggingEvent 格式化成字符串。格式化的結果依賴于 conversion 模式。

conversion 模式與 C 語言的 PRintf 函數很像。conversion 模式是由文字文本和格式控制表達式(稱為轉換指定符)組成。

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

每個轉換指定符(conversion specifier)以百分號(%)開始,后面跟一個可選的格式修飾符(format modifiers)和轉換模式(conversion pattern)名稱。 轉換模式名稱指定數據的類型,例如,日志器(logger)、級別(level)、日期(date)、線程名(thread name)。格式修飾符控制諸如,字段寬度,填充,左邊和右邊對齊等事情。

下面是一個簡單的例子:

轉換模式為 "%-5level [%thread]: %message%newline",并假設 log4net 環境設置為 PatternLayout,那么,下面語句:

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

將輸出,

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

需要注意的是,文字和轉換指示器之間沒有明確的分隔符。當模式解析器讀取一個轉換字符時,它知道何時已經達到了一個轉換指示器的末尾。上面的例子,在轉換指示符 %-5level 含義是,日志事件的級別占 5 個字符的寬度,并且左邊對齊。

下表是可以識別的轉換模式名稱:

轉換模式名稱

效果

a

等價于 appdomain

appdomain

用于輸出當產生日志事件時的 AppDomain 名稱。

aspnet-cache

指定 %aspnet-cache 時輸出所有 cache 項;指定 %aspnet-cache{key} 時只輸出 key 指定的項。

該模式對 Compact Framework or Client Profile 不可用。

aspnet-context

指定 %aspnet-context 時輸出所有 context 項;指定 %aspnet-context 時只輸出 key 指定的項。

該模式對 Compact Framework or Client Profile 不可用。

aspnet-request

指定 %aspnet-request 時輸出所有請求參數的項;指定 %aspnet-request{key} 時只輸出 key 指定的項。

該模式對 Compact Framework or Client Profile 不可用。

aspnet-session

指定 %aspnet-session 時輸出所有 session 項;指定 %aspnet-session{key} 時只輸出 key 指定的項。

該模式對 Compact Framework or Client Profile 不可用。

c

等價于 logger

C

等價于 type

class

等價于 type

d

等價于 date

date

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

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

這寫專用的日期格式化要比 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

輸出發生日志請求的文件名。

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

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

identity

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

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

l

等價于 location

L

等價于 line

location

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

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

位置信息很有用。然而,它的產生相當慢。除非執行速度不是問題,否則要避免使用。

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

level

輸出日志事件的級別。

line

輸出發生日志請求時的行號。

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

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

等價于 message

M

等價于 method

message

輸出與日志事件相關聯的應用程序提供的信息,也就是你敲入的信息。

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

輸出發生日志請求時的方法名。

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

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

n

等價于 newline

newline

輸出換行。換行是平臺依賴的,各個平臺可能不同。

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

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

p

等價于 level

P

等價于 property

properties

等價于 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

等價于 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

等價于 thread

timestamp

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

thread

輸出產生日志事件的線程名稱。如果沒有可用的線程名稱,則使用數字。

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".

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

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

u

等價于 identity

username

輸出當前活躍用戶的 WindowsIdentity。

警告:產生調用位置信息相當慢。除非執行速度不是問題,否則要避免使用它。

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

等價于 username

x

等價于 ndc

X

等價于 mdc

%

連續兩個百分號 %% 會輸出一個百分號。

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.

格式修飾符

左邊對齊

最小寬度

最大寬度

備注

%20logger

false

20

若日志器的名稱小于 20 個字符,則左邊補空格。

%-20logger

true

20

若日志器的名稱小于 20 個字符,則右邊補空格。

%.30logger

NA

30

若日志器的名稱大于 30 個字符,則截取。

%20.30logger

false

20

30

若日志器的名稱小于 20 個字符,則左邊補空格。但是,若日志器名稱的長度大于 30 個字符,則截取。

%-20.30logger

true

20

30

若日志器的名稱小于 20 個字符,則右邊補空格。但是,若日志器名稱的長度大于 30 個字符,則截取。

關于調用者位置信息(caller location information)
模式 %type、%file、%line、%method、%location、%class、%C、%F、%L、%l 和 %M 都會產生調用者位置信息。位置信息使用 System.Diagnostics.StackTrace 類來產生調用堆棧。調用者信息是從這個堆棧提取的。

注意

.NET Compact Framework 1.0 不支持 System.Diagnostics.StackTrace 類,因此,調用者位置信息也是不可用的。

注意

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

上一篇:使用Builder模式造車

下一篇:DataTable操作

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
一区二区三区www| 中文字幕精品—区二区| 亚洲xxxxx电影| 国产精品一区二区三| 日韩精品在线免费播放| 综合激情国产一区| 国产成人高潮免费观看精品| 夜夜嗨av色综合久久久综合网| 久久久最新网址| 国产精品成人v| 国产亚洲成av人片在线观看桃| 中文字幕av日韩| 亚洲在线观看视频| 精品在线观看国产| 亚洲精品久久久久久久久久久久| 国产精品91视频| 91久久久久久久久久| 欧美色另类天堂2015| 91久久嫩草影院一区二区| 欧美成人三级视频网站| 国产性猛交xxxx免费看久久| 日韩欧美国产黄色| 97**国产露脸精品国产| 久久成人精品视频| 亚洲成人网在线观看| 亚洲成av人影院在线观看| 亚洲男人天堂2019| 91久久久久久久久久| 91久久精品美女| 亚洲激情久久久| 在线a欧美视频| 亚洲精品福利在线| 国产成人avxxxxx在线看| 日韩美女av在线免费观看| 亚洲男人第一av网站| 中文字幕在线精品| 欧美大片网站在线观看| 91久久久久久久久久| 浅井舞香一区二区| 国产91精品网站| 日韩激情在线视频| 91a在线视频| 成人在线中文字幕| 亚洲精品国精品久久99热一| 久久天堂av综合合色| 亚洲一区二区日本| 国产精品久久在线观看| 岛国av一区二区| 亚洲欧洲国产伦综合| 亚洲成人性视频| 久久影院在线观看| 日韩国产在线看| 中文字幕日韩欧美在线视频| 粉嫩老牛aⅴ一区二区三区| 亚洲性日韩精品一区二区| 成人欧美一区二区三区黑人| 亚洲综合精品伊人久久| 少妇久久久久久| 97色在线播放视频| 91精品国产高清| 亚洲久久久久久久久久| 亚洲男人av在线| 91精品国产91久久久久久久久| 日韩视频免费观看| 国产亚洲在线播放| 国产精品亚发布| 亚洲乱码国产乱码精品精| 精品久久久久久中文字幕一区奶水| 亚洲韩国欧洲国产日产av| 久久视频免费观看| 久久久久国产视频| 欧美有码在线观看| 国产精品av电影| 日本久久久久亚洲中字幕| 69影院欧美专区视频| 亚洲激情在线观看视频免费| 在线亚洲午夜片av大片| 自拍亚洲一区欧美另类| 丝袜亚洲欧美日韩综合| 久久久精品在线观看| 青青在线视频一区二区三区| 日韩免费看的电影电视剧大全| 成人精品在线观看| 久久久亚洲国产| 欧美亚洲伦理www| 国产网站欧美日韩免费精品在线观看| 亚洲国产精品久久久久久| 欧美成人一区在线| 国模视频一区二区| 欧美日韩精品二区| 日本韩国在线不卡| 亚洲最新中文字幕| 91精品国产一区| 亚洲欧美第一页| 自拍偷拍亚洲一区| 成人免费在线网址| 国产午夜精品久久久| 欧美重口另类videos人妖| 亚洲免费av网址| 亚洲一品av免费观看| 日韩精品免费观看| 久久99久久99精品免观看粉嫩| 久久99精品久久久久久噜噜| 亚洲欧美制服中文字幕| 久久精品国产69国产精品亚洲| 亚洲精品98久久久久久中文字幕| 色悠悠国产精品| 亚洲在线www| 亚洲第一网中文字幕| 精品久久久久久国产91| 亚洲激情视频在线播放| 国产精品一区二区av影院萌芽| 91青草视频久久| 红桃av永久久久| 亚洲国产福利在线| 国产精品草莓在线免费观看| 中文字幕日韩高清| 日韩电影免费观看在线观看| 欧美性猛交xxxx乱大交极品| 成人情趣片在线观看免费| 在线观看亚洲视频| 色综久久综合桃花网| 国产精品日韩久久久久| 97成人精品视频在线观看| 91免费精品国偷自产在线| 亚洲国产成人久久综合一区| 国产香蕉精品视频一区二区三区| 一级做a爰片久久毛片美女图片| 日韩精品视频免费在线观看| 日韩欧美国产一区二区| 国产九九精品视频| 日韩在线视频观看| 亚洲欧美制服综合另类| 第一福利永久视频精品| 日韩在线中文字| 日本一区二区不卡| 欧美电影在线播放| 亚洲欧美日韩中文在线制服| 日产精品久久久一区二区福利| 久久久久99精品久久久久| 日韩影视在线观看| 日本人成精品视频在线| 亚洲国产精品va在看黑人| 69av在线视频| 国产91亚洲精品| 日韩成人小视频| 国产精品白嫩初高中害羞小美女| 欧美色道久久88综合亚洲精品| 日韩欧美福利视频| 国产精品私拍pans大尺度在线| 国产午夜精品久久久| 亚洲国产97在线精品一区| 中文字幕av日韩| 亚洲精品久久久久久久久久久久久| 最近2019免费中文字幕视频三| 精品久久久久久国产91| 亚洲国产精品高清久久久| 精品久久香蕉国产线看观看gif| 98精品在线视频| 人人做人人澡人人爽欧美| 精品久久久久久久久久久久久久| 在线观看国产精品日韩av| 最近中文字幕2019免费| 精品国内亚洲在观看18黄|