Log4net PatternLayout 参数
Log4net PatternLayout 参数
来自: https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Layout.PatternLayout.html
A flexible layout configurable with pattern string.
For a list of all members of this type, see PatternLayout Members.
System.Object
���log4net.Layout.LayoutSkeleton
������log4net.Layout.PatternLayout
���������log4net.Layout.DynamicPatternLayout
[Visual�Basic]
Public�Class�PatternLayout
����Inherits�LayoutSkeleton
[C#]
public�class�PatternLayout : LayoutSkeleton
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.
Remarks
The goal of this class is to Format a LoggingEvent as a string. The results depend on the conversion pattern.
The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.
You are free to insert any literal text within the conversion pattern.
Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion pattern name. The conversion pattern name specifies the type of data, e.g. logger, level, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example.
Let the conversion pattern be "%-5level [%thread]: %message%newline" and assume that the log4net environment was set to use a PatternLayout. Then the statements
[C#]
ILog log = LogManager.GetLogger(typeof(TestApp));
log.Debug("Message 1");
log.Warn("Message 2");
would yield the output
DEBUG [main]: Message 1
WARN [main]: Message 2
Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5level means the level of the logging event should be left justified to a width of five characters.
The recognized conversion pattern names are:
|
Conversion Pattern Name |
Effect |
|
a |
Equivalent to appdomain |
|
appdomain |
Used to output the friendly name of the AppDomain where the logging event was generated. |
|
aspnet-cache |
Used to output all cache items in the case of %aspnet-cache or just one named item if used as %aspnet-cache{key} This pattern is not available for Compact Framework or Client Profile assemblies. |
|
aspnet-context |
Used to output all context items in the case of %aspnet-context or just one named item if used as %aspnet-context{key} This pattern is not available for Compact Framework or Client Profile assemblies. |
|
aspnet-request |
Used to output all request parameters in the case of %aspnet-request or just one named param if used as %aspnet-request{key} This pattern is not available for Compact Framework or Client Profile assemblies. |
|
aspnet-session |
Used to output all session items in the case of %aspnet-session or just one named item if used as %aspnet-session{key} This pattern is not available for Compact Framework or Client Profile assemblies. |
|
c |
Equivalent to logger |
|
C |
Equivalent to type |
|
class |
Equivalent to type |
|
d |
Equivalent to date |
|
date |
Used to output the date of the logging event 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. 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. |
|
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 |
Equivalent to file |
|
file |
Used to output the file 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. |
|
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 |
Equivalent to location |
|
L |
Equivalent to 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 |
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". WARNING Generating the caller class information is slow. Thus, its use should be avoided unless execution speed is not an issue. 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 |
|
% |
The sequence %% outputs a single percent sign. |
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.
|
Format modifier |
left justify |
minimum width |
maximum width |
comment |
|
%20logger |
false |
20 |
none |
Left pad with spaces if the logger name is less than 20 characters long. |
|
%-20logger |
true |
20 |
none |
Right pad with spaces if the logger name is less than 20 characters long. |
|
%.30logger |
NA |
none |
30 |
Truncate from the beginning if the logger name is longer than 30 characters. |
|
%20.30logger |
false |
20 |
30 |
Left pad with spaces if the logger name is shorter than 20 characters. However, if logger name is longer than 30 characters, then truncate from the beginning. |
|
%-20.30logger |
true |
20 |
30 |
Right pad with spaces if the logger name is shorter than 20 characters. However, if logger name is longer than 30 characters, then truncate from the beginning. |
Note about caller location information.
The following patterns %type %file %line %method %location %class %C %F %L %l %M all generate caller location information. Location information uses the System.Diagnostics.StackTrace class to generate a call stack. The caller's information is then extracted from this stack.
CAUTION���
The System.Diagnostics.StackTrace class is not supported on the .NET Compact Framework 1.0 therefore caller location information is not available on that framework.
CAUTION���
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.
Example
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
Requirements
Assembly: log4net (in log4net.dll)
Log4net PatternLayout 参数的更多相关文章
- Log4Net各参数API
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- log4j - 输出格式控制, PatternLayout参数含义以及详细配置
转载自:https://blog.csdn.net/reserved_person/article/details/52849505 做项目被log4j的输出格式化参数搞烦了,索性把API的相关部分大 ...
- Log4j输出格式log4j的PatternLayout参数含义
摘自:http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html 参数 说明 例子 %c 列出logger ...
- Log4j输出格式控制--log4j的PatternLayout参数含义
参数 说明 例子 %c 列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间 log4j配置文件参数举例 输出显示媒介 假设当前logger名字 ...
- log4j的PatternLayout参数含义
参数 说明 例子 %c 列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间 log4j配置文件参数举例 输出显示媒介 假设当前logger名字空间 ...
- 搭建一套自己实用的.net架构(2)【日志模块-log4net】
先谈谈简单的模块,日志.在系统中日志模块是必须的,什么系统日志,操作日志,调试日志.这里用的是log4net. 对log4net还不熟悉的小伙伴们赶快去搜索基础教程哦, 我这里就不温故了. 那么有人要 ...
- Web 应用配置Log4Net
1.第一步:在web.config文件添加如下代码: [sourcecode language="csharp"] <configSections> <secti ...
- C# 使用 log4net 记录日志
Ø 前言 在一般的开发应用中,都会涉及到日志记录,用于排查错误 或 记录程序运行时的日志信息.log4net 库是 Apache log4j 框架在 Microsoft .NET 平台的实现,是一个 ...
- NetCore log4net 集成以及配置日志信息不重复显示或者记录
NetCore log4net 集成,这是一个很常见而且网上大批大批的博文了,我写这个博文主要是为了记录我在使用过程中的一点小收获,以前在使用的过程中一直没有注意但是其实网上说的不清不楚的问题. 官方 ...
随机推荐
- [Asp.net core]使用Polly网络请求异常重试
摘要 在网络传输过程中,不能保证所有的请求都能正确的被服务端接受或者处理,那么进行简单的重试可以进行简单的补救.比如现在大部分支付功能,在支付成功之后,需要回调我们网站的接口,并且要求我们的接口给一个 ...
- .Net Discovery 系列之一--string从入门到精通(上)
string是一种很特殊的数据类型,它既是基元类型又是引用类型,在编译以及运行时,.Net都对它做了一些优化工作,正式这些优化工作有时会迷惑编程人员,使string看起来难以琢磨,这篇文章分上下两章, ...
- NSArray进行汉字排序
由于NSArray并不直接支持对汉字的排序,这就要通过将汉字转换成拼音完毕按A~Z的排序,这看起来是个头疼的问题.由于牵扯到汉字转为拼音,kmyhy给出一个较易实现的方法,获取汉字的首字的首字母,如将 ...
- 咏南Mormot中间件接口
咏南Mormot中间件接口 只使用了MORMOT的HTTPS.SYS作为通讯,数据引擎使用FIREDAC,数据序列/还原是自行封装. 客户端支持FDMemeTable和ClientDataSet数据集 ...
- C#编程(十二)----------函数
类和结构 类和结构实际上都是创建对象的模板 ,每 个对象都包含数据 ,并 提供了处理和访问数据的方法. 类定义了类的每个对象 (称 为实例 )可 以包含什么数据和功能 . 例如 ,如 果 一 个类表示 ...
- jQuery Ajax方式上传文件实现暂停或取消上传
未上传时要实现取消,很简单... 但如果用户点击了上传,并加载了进度信息... 2017-05-04再次改进.在上传过程中用户可以按 Esc 来取消上传(取消当前上传,或者是全部上传)... 也可以在 ...
- Work Management Service application in SharePoint 2016
最近开始弄SharePoint 2016的Workflow,遇到问题发现没有了Work Management Service application,然后用PowerShell命令创建也不行,bing ...
- Wireshark基本用法 && 过滤规则 && 协议详解
基本使用: https://www.cnblogs.com/dragonir/p/6219541.html 协议解析: https://www.jianshu.com/p/a384b8e32b67 ( ...
- [Web 前端] SuperAgent中文使用文档
cp from : https://blog.csdn.net/gebitan505/article/details/58585846 superagent是nodejs里一个非常方便的客户端请求代理 ...
- HTML5 本地文件操作之FileSystemAPI实例(一)
文件操作实例整理一 1.请求系统配额类型 console.info(window.TEMPORARY); //0 临时 console.info(window.PERSISTENT); //1 持久 ...