Log4net, c # log4net
Reference page:
Http://www.yuanjiaocheng.net/CSharp/csharprumenshili.html
Http://www.yuanjiaocheng.net/entity/mode-first.html
Http://www.yuanjiaocheng.net/entity/database-first.html
Http://www.yuanjiaocheng.net/entity/choose-development-approach.html
Http://www.yuanjiaocheng.net/entity/query-with-edm.html
In the project process, you must use the log function. I have made a set of logs for my projects, but I still use other mature dll, such as log4.
Log4-related documents are really many and comprehensive, but I still want to repeat this for the purpose of understanding new things.
First, put a small Demo on it.
<appender name="TraceLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logs\trace.txt" /> <MaxSizeRollBackups value="20" /> <StaticLogFileName value="false" /> <DatePattern value="yyyyMMdd'.txt'" /> <RollingStyle value="Size" /> <MaximumFileSize value="10MB"/> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout">
There are five main components: Appenders, Filters, Layouts, Loggers, Object Renders
1. Appenders
Defines the log output mode. You can configure Filters and Layout to filter logs and output formats.
There are many output methods. Here we mainly introduce the most commonly used methods in several projects.
AdoNetAppender |
Record logs to the database |
FileAppender |
Output logs to files |
RollingFileAppender |
Write the log to the file as a rollback File |
SmtpAppender |
Write logs to emails |
2. Filters
The filter can filter out the content output by Appender.
DenyAllFilter |
Prevent all log events from being recorded |
LevelMatchFilter |
Only log events of the specified level are recorded. |
LevelRangeFilter |
Events with a log level within a specified range are recorded. |
LoggerMatchFilter |
Logger name match. |
PropertyFilter |
The message is recorded only when it matches the specified property value. |
StringMathFilter |
The specified string is matched by the message. |
3. Layouts
It is used to control the output format of Appender, which can be linear or Xml. however, one Appender can have only one Layout. after this is configured, you do not need to customize a format in the program.
The most commonly used PatternLayout is the custom format. Next, let's take a look at the format and meaning in ConversionPattern.
<ConversionPattern value="%d [%t] %-5p %c - %m%n" />
A Appdomain |
Name of the application domain that raises the log event |
C (lower case) Logger |
Name of the Logger object that Records log events You can use the precision specifier to control the output level of the Logger name. By default, the output full name is used. Note that the control of the precision character starts from the right. For example, the logger name is "a. B. c", and the output model is% Logger {2}To output "B. c" |
C (uppercase) Class Type |
Full name of the class that raises the log request You can use the precision control character. For example, the class name is "log4net. Layout. PatternLayout ", The format model is% Type {1}Output "PatternLayout ". (It also starts from the right .) Warning:Will affect the performance. |
D Date |
Record the current local time. For example, % d {HH: mm: ss, fff} or % d {dd MMM yyyy HH: mm: ss, fff} We recommend that you use a custom time format. |
Exception |
Exception Information An exception object must be stored in the log. If the log event does not contain any exception object, nothing can be output. After the exception output is completed, a line feed will be followed. A line feed will be added before the output exception, and the exception will be placed at the end. |
F File |
Name of the source code file in which a log request occurs Warning:It is only valid during debugging. Calling local information affects performance. |
U Identity |
Name of the current active user (Principal. Identity. Name ). Warning:Will affect the performance. (% Identity returned during the test is empty .) |
L (lower case of L) Location |
Method for triggering a log event (including the namespace and class name), as well as the source file and row number. Warning:Will affect the performance. If there is no pdb file, there is only the method name, and there is no source file name and line number. |
L P Level |
Log Level |
Line |
The row number of the log. |
M Message |
Messages provided by applications to logs |
M Method |
Method Name When a log request occurs (only the method name is used ). Warning:Will affect the performance. |
X Mdc P Property Propertites |
Special attribute of the output event. For example:% Property {user}Output the user attribute. The attribute is added to the time by loggers or appenders. There is always a default attribute "DE <log4net: HostName. DE <% PropertyWill output so attributes. |
N Newline |
Line Break |
R Timestamp |
The number of milliseconds from program startup to event occurrence. |
T Thread |
The thread that generates a log event. If there is no thread name, use the thread number. |
W Username |
WindowsIdentity of the current user. (For example, HostName \ Username) Warning:Will affect the performance. |
Utcdate |
The UTC time when the log event occurred. DE <can be followed by a date format and enclosed in braces. DE <example:% Utcdate {HH: mm: ss, fff}Or% Utcdate {dd MMM yyyy HH: mm: ss, fff}. If utcdate is followed by nothing, the ISO8601 format is used. The date format is the same as that used in the ToString method of the DateTime class in. Net. In addition, log4net has three Formatter formats. They are represented by "ABSOLUTE", "DATE", and "ISO8601 ".AbsoluteTimeDateFormatter,DateTimeDateFormatterAndIso8601DateFormatter. For example:% Date {ISO8601}Or% Date {ABSOLUTE}. Their performance is better than that of ToString. |
X Ndc |
NDC (nested diagnostic context) |
% |
% Output a percent sign |
What does-5 in the demo above mean? See
Format modifier |
Left justify |
Minimum width |
Maximum width |
Comment |
% 20 logger |
False |
20 |
None |
If the logger name is less than 20 charactersLeftFill in spaces. |
%-20 logger |
True |
20 |
None |
If the logger name is less than 20 charactersRight sideFill in spaces. |
%. 30 logger |
NA |
None |
30 |
More than 30 characters are truncated. |
% 20.30 logger |
False |
20 |
30 |
The logger name must be in the range of 20 to 30.LeftFill in space, with more truncation. |
%-20.30 logger |
True |
20 |
30 |
The logger name must be in the range of 20 to 30.Right sideFill in space, with more truncation. |
4. Loggers
Logger is a component that interacts directly with the application. Logger only generates logs, and then records the Appender referenced by it to the specified place, and controls the output format with Layout.
Logger provides multiple methods to record one log message, and multiple Logger may exist at the same time. Each instantiated Logger object is maintained by log4net as a naming entity.
Log4net uses the inheritance system. Two Logger, a and a. B, and a are the ancestor of a. B. Each Logger inherits the attributes of its ancestor.
The log levels are as follows:
OFF> FATAL> ERROR> WARN> INFO> DEBUG> ALL.
5. Object Renders
This is my understanding of Object Renders. It tells logger how to convert an object into a string and record it in the log. (You may notice that the interface defined in ILog receives an Object parameter instead of a String parameter .)
For example, if you want to record the Orange object to the log, logger will only call the Orange default ToString method. Therefore, define an OrangeRender class to implement the log4net. ObjectRender. IObjectRender interface, and then register it. Then logger will know how to record Orange to the log.
However, I have not tested it. For more information, see the document.
Refer:
Log4net User Manual (this document is copied from here and the link is more detailed)