Text format description
The log types that can be recorded include fatal, error, warn, info, and debug ).
Text parameter description
% M (Message): the output Log message, such as ilog. debug (...) Output Message
% N (New Line): line feed
% D (datetime): time when the current statement is output
% R (Run time): OutputProgramNumber of milliseconds consumed from running to execution to the current statement
% T (thread ID): The thread ID of the current statement is equivalent to % thread
% P (priority): Current Log priority level, namely debug, info, warn... And so on
% C (class): name of the current log object, for example:
Mode string: %-10C-% m % N
CodeIs:
Ilog log = logmanager. getlogger ("exam. Log ");
Log. debug ("hello ");
The output is in the following format:
Exam. Log-Hello
% L: the row number of the output statement
% F: File Name of the output statement
%-Number: indicates the minimum length of the item. If not, fill it with spaces.
% Newline: Error details
% Message: Custom output information
% Logger: Class
% Property: Property
The following is a personal Template
% N ============
% N [log level] %-5 level
% N [record time] % date
% N [thread number] [% thread]
% N [execution time] [% R] millisecond
% N [error file] % F
% N [error row number] % L
% N [error class] % logger property [% property {NDC}]
% N [error description] % message
% N [error details] % newline
Output example:
============
[Log level] Error
[Recording time] 15:49:37, 625
[Thread number] [10]
[Execution time] [180] milliseconds
[Error file] c: \ Users \ Administrator \ Documents \ Visual Studio 2010 \ projects \ consoleapplication5 \ consoleapplication5 \ Program. CS
[Error row number] 24
[Error class] leleapplication5.program attribute [(null)]
[Error description] Error
[Error details]
System. dividebyzeroexception: try to divide by zero.
In leleapplication5.program. Main (string [] ARGs) Location C: \ Users \ Administrator \ Documents ents \ Visual Studio 2010 \ projects \ consoleapplication5 \ consoleapplication5 \ Program. CS: row number 20
============
[Log level] Error
[Recording time] 15:49:37, 666
[Thread number] [10]
[Execution time] [221] milliseconds
[Error file] c: \ Users \ Administrator \ Documents \ Visual Studio 2010 \ projects \ consoleapplication5 \ consoleapplication5 \ Program. CS
[Error row number] 28
[Error class] leleapplication5.program attribute [(null)]
[Error description] Error
[Error details]
System. Exception: An exception occurred.
Instructions for use:
Configuration file:
Node: Add in <configuration>
<Log4net> <! -- Define output to file --> <appender name = "sysappender" type = "log4net. appender. rollingfileappender"> <! -- Log Path --> <file value = "logs/log4net/"/> <! -- Overwrite or not. The default value is true. --> <appendtofile value = "true"/> <rollingstyle value = "composite"/> <! -- File name --> <datepattern value = "yyyy-mm-dd'. log'"> </datepattern> <! -- Set unlimited backup =-1 and the maximum number of backups is 1000 --> <Param name = "maxsizerollbackups" value = "1000"/> <! -- Size of each file --> <Param name = "maximumfilesize" value = "500kb"/> <! -- Can the name be changed to false? --> <Param name = "staticlogfilename" value = "false"/> <layout type = "log4net. layout. patternlayout"> <! -- Output format --> <conversionpattern value = "% N ===========% N [log level] %-5 level % N [record time] % date % N [thread number] [% thread] % N [execution time] [% R] millisecond % N [error file] % F % N [error row number] % L % N 【 error class] % logger attribute [% property {NDC}] % N [error description] % message % N [error details] % newline "/> </layout> </appender> <! -- Define the output to the console command line --> <appender name = "leleappender" type = "log4net. appender. consoleappender "> <layout type =" log4net. layout. patternlayout "> <conversionpattern value =" % N ===========% N [log level] %-5 level % N [record time] % date % N [thread number] [% thread] % N [execution time] [% R] millisecond % N [error file] % F % N [error row number] % L % N [Error class] % logger attribute [% property {NDC}] % N [error description] % message % N [error details] % newline "/> </layout> </appender> <! -- Define output to Windows events --> <appender name = "eventlogappender" type = "log4net. appender. eventlogappender "> <layout type =" log4net. layout. patternlayout "> <conversionpattern value =" % N ===========% N [log level] %-5 level % N [record time] % date % N [thread number] [% thread] % N [execution time] [% R] millisecond % N [error file] % F % N [error row number] % L % N [Error class] % logger attribute [% property {NDC}] % N [error description] % message % N [error details] % newline "/> </layout> </appender> <! -- Define the log output media. The following defines four log output methods. You can also output data of the following type or other types. --> <Root> <! -- Log recorded as a file --> <appender-ref = "sysappender"/> <! -- Console control display log --> <appender-ref = "leleappender"/> <! -- Windows event log --> <! -- <Appender-ref = "eventlogappender"/> --> <! -- If the corresponding logging is not enabled, you can comment out <appender-ref = "adonetappender_access"/> --> </root> </log4net>
Code:
Reference
Using log4net;
Using system. Reflection
Add [Red content] To the namespace
[Assembly: log4net. config. xmlconfigurator (watch = true)]
Namespace consoleapplication5
Call example:
using system; using system. collections. generic; using system. text; using log4net; using system. reflection; [Assembly: log4net. config. xmlconfigurator (watch = true)] namespace consoleapplication5 {class program {static void main (string [] ARGs) {// create a logging component instance ilog log = log4net. logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype); // record a serious error log. fatal ("severe error"); log. fatal ("severe error", new exception ("a fatal error occurred"); // record the error log. error ("error"); log. error ("error", new exception ("an exception occurred"); // log the warning information. warn ("warning"); log. error ("warning", new exception ("there is a warning message"); // record general information log. info ("general information"); log. error ("general information", new exception ("issuing a general information"); // record debugging information log. debug ("debug information"); log. error ("debugging information", new exception ("A debugging information occurred"); console. read () ;}}
Download the sample program: log plug-in _log4net.zip