These days in learning the use of log4net, the following is the study notes:
1. Download and installation of log4net
Official website or degree Niang download source file compilation, or directly download DLL, the introduction of engineering can.
Configuration of the 2.log4net
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="log4net"type="log4net. Config.log4netconfigurationsectionhandler,log4net"/> </configSections> <!--log4net Log configuration section--<log4net> <root> <!--control level from low to High: All|deb ug|info| warn| error| fatal| Off--> <!--For example, if the level is defined as info, the level of the info level downward, such as the debug log will not be recorded--<!--If there are no values defined for levels, the default is Debug--> <le Vel value="ERROR"/> <appender-ref ref="Rollingfileappender"/> </root> <logger name="PROGRAM.A"> <level value="DEBUG"/> <appender-ref ref="Rollingfileappender"/> </logger> <logger name="Program.b"> <level value="DEBUG"/> <appender-ref ref="Rollingfileappender"/> </logger> <appender name="Rollingfileappender"Type="log4net. Appender.rollingfileappender"> <!--log file name Start-to-<file value="D:\Log\TestLog4net.TXT"/> <!--multi-threading with minimal lock-to-<lockingmodel type="log4net. Appender.fileappender+minimallock"/> <!--date format, change every day for a file record, if not set then always log only one day, need to set--<datepattern value="(YYYYMMDD)"/> <!--append to file, default to True, usually without setting--<appendtofile value="true"/> <!--transform in the form of a date, in which case there is only one log--<!--at this time the node settings for maxsizerollbackups and maximumfilesize are meaningless--<!- -<rollingstyle value="Date"/>--> <!--transform in the form of log size-<!--in this case the node settings for maxsizerollbackups and maximumfilesize are meaningful--<rolli Ngstyle value="Size"/> <!--the number of log files logged per day, using maximumFileSize with <maxsizerollbackups value="Ten"/> <!--maximum size of each log file-<!--available units: kb| Kb: Gb--> <!--do not use decimals, otherwise it will always be written to the current log--<maximumfilesize value="2MB"/> <!--log format-<layout type="log4net. Layout.patternlayout"> <conversionpattern value="%date [%t]%-5p%c-%m%n"/> </layout> </appender> </log4net></configuration>
3. Test the code:
[Assembly:log4net. Config.xmlconfigurator (Watch =true)]namespacelog4netstu{classProgram {Static voidMain (string[] args) {Console.WriteLine ("BEGIN"); //ILog m_log = Logmanager.getlogger ("SSS"); //M_log. Error ("This is an error log");Thread T1=NewThread (start); T1. Name="A"; T1. Start (); Thread T2=NewThread (start); T2. Name="B"; T2. Start (); Console.ReadLine (); } Public Static voidstart () {stringName ="Program ."+Thread.CurrentThread.Name; ILog M_log=Logmanager.getlogger (name); Console.WriteLine (Name+"Start"); for(inti =0; I <= +; i++) {M_log. Debug ("This is a debug log"); Thread.Sleep ( -); } Console.WriteLine (Name+"End"); } }}
4. Test results:
2015-12-15 09:59:45,790 [A]debug PROGRAM.A-This is a DEBUG log
2015-12-15 09:59:45,790 [B]debug program.b-This is a DEBUG log
2015-12-15 09:59:45,790 [B]debug program.b-This is a DEBUG log
2015-12-15 09:59:45,790 [A]debug PROGRAM.A-This is a DEBUG log
2015-12-15 09:59:48,974 [B]debug program.b-This is a DEBUG log
2015-12-15 09:59:48,974 [B]debug program.b-This is a DEBUG log
2015-12-15 09:59:48,974 [A]debug PROGRAM.A-This is a DEBUG log
5. Configuration details (learn where to update to ...)
Layout output Format:
|
Conversion characters |
Effect |
|
A |
equals to the AppDomain . |
|
AppDomain |
Used to output the friendly name of the AppDomain where the logging event is generated.
A friendly name for the AppDomain that produced the journal event |
|
C |
Equivalent to logger |
|
C |
Equivalent to type |
|
Class |
Equivalent to type |
|
D |
Equivalent to date |
|
Date |
Used to output the date of the event in the current zone. To output a universal time, use the%utcdate mode. The date conversion specifier can be followed by a pair of enclosed flower parentheses, with a date format specifier in the flower notation. For example:%date{hh:mm:ss,fff} or %date{dd MMM yyyy hh:mm:ss,fff}, if no format is specified, the default is ISO8601 (Iso8601dateformatter).
The format specifier is the same as the language of ToString.
To achieve better results, it is recommended to use the Log4net date formatter. They can use one of the following strings, "ABSOLUTE", "date", "ISO8601" to specify Absolutetimedateformatter, Datetimedateformatter and Iso8601dateformatter. For example :%date{iso8601} or %date{absolute}.
These date formatter effects are much better than ToString. |
|
exception |
It is used to import the same as the same day magazine information.
If an unusual image is stored in a journal event, he will be given a pattern with trailing newline. No, No. |
|
F |
Equivalent to file |
|
File |
Used to enter the name of the file to be found in the journal.
Example: C:\inetpub\wwwroot\logtest\webform1.aspx.cs
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
Identity |
Used to output the user name of the former user (Principal.Identity.Name).
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
L |
Equivalent to location |
|
L |
Equivalent to line |
|
Location |
Used for the location information of the caller who produced the event for the birthday sign.
This location information is dependent on the CLI, but he is typically named for space-constrained methods, followed by a caller's file name and line number that is enclosed in parentheses.
Example: LogTest.WebForm1.Button3_Click (c:\inetpub\wwwroot\logtest\webform1.aspx.cs:93)
Location information is very useful. However, his production integration processes is slow. Avoid using as much as you can, unless the speed is not a problem. |
|
Level |
Used to enter the rating of the recording event. |
|
Line |
Used to enter the line number to be asked for the date of the introduction.
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
Logger |
Used to enter the journal name of the journal event. This journal can be followed by the precision, the precision is a ten-in-one constant, surrounded by a flower. If you specify a precision character, you will only be given a right-to-left phase of the target name, which defaults to entering all.
As follows: The name of the journal Recorder is "A.B.C", %logger{2} will be output "B.C" |
|
M |
is equivalent to a message |
|
M |
Equal to method |
|
Message |
Information that is used in the output application to provide a journal event. |
|
Mdc |
The MDC (old name: Threadcontext.properties) is part of the Union event. This mode supports compatibility but is equivalent to property. |
|
Method |
Use the name of the method used to produce a journal.
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
N |
Equivalent to newline |
|
NewLine |
The same as "\ n" or "\ r \ n". |
|
P |
Equal to level |
|
P |
Equivalent to property |
|
Properties |
Equivalent to property |
|
Property |
Used to output the specified properties of an event. The name of the property should be enclosed in a flower notation and placed directly behind it. For example:%property{user}. Each of the symbols in the journal requires a single designation. The properties are added to the event by the journal Recorder or the add-in. The implied property is Log4net:hostname, which is the name of the machine that produced the recording event.
If no keyword is specified, for example: %property , the key value will be divided by the number of points sorting |
|
R |
Equivalent to timestamp |
|
T |
equals thread . |
|
Timestamp |
Used to output the time from program activation to the creation of a journal event, in milliseconds. |
|
Thread |
For the name of the thread that is used to produce the journal event, use the thread if the thread name is not available. |
|
Type |
The same logger used
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
U |
Equal to identity |
|
Username |
Used to WindowsIdentity the former user
Warning : The location information of the production caller is slow. Try to avoid it, unless the speed is not a problem. |
|
Utcdate |
Use the same date |
|
W |
Equivalent to username |
|
X |
Equal to NDC |
|
X |
Equivalent to the MDC |
|
% |
Percent output a% |
6. Frequently Asked Questions
1) After introduction, the configuration is correct but unable to write the log.
The reason may be that this code is missing in front of the table space [Assembly:log4net. Config.xmlconfigurator (Watch = True)]
2) The above program will repeat the print log
The reason is that Appender is specified for root logger (root) and its child logger (PROGRAM.A and PROGRAM.B). The behavior of the parent logger will still take effect when the child logger is called, so root and PROGRAM.A and program.b output the same log
Log4net Study Notes