- Download log4net Apache log4net-download Apache log4net
-
- Modify assemblyinfo. CS
Add [Assembly: log4net. config. domconfigurator (configfile = "log. log4net", configfileextension = "log4net", watch = true)]
-
- Add your own logger. Log component single-piece mode in the project.
Public class Logger
{
Private Static log4net. ilog logger = log4net. logmanager. getlogger ("all ");
Public static void log (string message)
{
Logger. debug (Message );
}
// Add the appropriate log reload for the project here, such as public static void log (string message, exception ex)
}
- The log. log4net file mentioned above first posts my configuration in the project:
<Log4net>
<Root>
<Level value = "all"/>
<Appender-ref = "rollingfile"/>
</Root>
<Appender name = "rollingfile" type = "log4net. appender. rollingfileappender, log4net">
<Param name = "file" type = "" value = "log"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyy-mm-DD & quot;. log & quot;"/>
<Param name = "maxsizetorollbackups" value = "10"/>
<Param name = "maximumfilesize" value = "5 MB"/>
<! -- Static and Dynamic log file name -->
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "& #13; & #10; ------------------------ header -------------------------- & #13; & #10;"/>
<Param name = "footer" value = "& #13; & #10; ------------------------ footer -------------------------- & #13; & #10;"/>
</Layout>
</Appender>
<Appender name = "leleapp" type = "log4net. appender. leleappender, log4net">
<Layout type = "log4net. layout. patternlayout, log4net">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
</Layout>
</Appender>
<Logger name = "log4nettest. logtest">
<Level value = "debug"/>
<Appender-ref = "rollingfile"/>
<Appender-ref = "coloredconsoleapp"/>
<Appender-ref = "systemevent"/>
</Logger>
</Log4net>
I have been using log4net in development projects, but I have never been used to writing notes, what I have done, what I have learned, and what I have found. I will share with you here.
Log4net details the conversion mode (conversionpattern) in log4net. layout. patternlayout)
% 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): ID of the thread where the current statement is located
% P (priority): Current Log priority level, namely debug, info, warn... And so on
% C (class): name of the current log object
% 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.
For example, execute logger in patternlayout in the conversion mode of % d [% T] %-5 p % C-% m % N. log ("Hello"); generates outputs similar to the following:
09:55:11, 421 [2720] Debug all-hello.