The Config configuration file is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Configsections >
< Section Name = "Log4net" Type = "Log4net. config. log4netconfigurationsectionhandler, log4net" />
</ Configsections >
< Log4net >
< Appender Name = "Rollingfile" Type = "Log4net. appender. rollingfileappender" >
< File Value = "Log/log.txt" />
< Appendtofile Value = "True" />
< Maximumfilesize Value = "300kb" />
< Maxsizerollbackups Value = "2" />
< Layout Type = "Log4net. layout. patternlayout" >
< Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
</ Layout >
</ Appender >
< Appender Name = "Loginfile" Type = "Log4net. appender. rollingfileappender" >
< File Value = "Log/login.txt" />
< Appendtofile Value = "True" />
< Maximumfilesize Value = "300kb" />
< Maxsizerollbackups Value = "2" />
< Layout Type = "Log4net. layout. patternlayout" >
< Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
</ Layout >
</ Appender >
< Root >
< Level Value = "All" />
< Appender-ref Ref = "Rollingfile" />
</ Root >
< Logger Name = "Loginfile" >
< Level Value = "All" />
< Appender-ref Ref = "Loginfile" />
</ Logger >
</ Log4net >
</ Configuration >
Multiple logger and appender sections can be added for use.
<Level value = "all"/> indicates that the following media is used for prompts of all levels of warn, debug, and info.
Server Code As follows: Namespace Log4net
{
/**/ /// <Summary>
///Summary Description for class1.
/// </Summary>
Class Class1
{
/**/ /// <Summary>
///The main entry point for the application.
/// </Summary>
///
Public Static Readonly Log4net. ilog log2 = Log4net. logmanager. getlogger ( " Loginfile " );
Public Static Readonly Log4net. ilog = Log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );
[Stathread]
Static Void Main ( String [] ARGs)
{
Log2.info ( " ---- Info ---- " );
Log2.warn ( " ---- Info ---- " );
Log2.debug ( " ---- Info ---- " );
Log. Info ( " ---- Info ---- " );
Log. Warn ( " ---- Info ---- " );
Log. debug ( " ---- Info ---- " );
}
}
}
Log4netThe Framework definesLogmanagerTo manage allLoggerObject. It hasGetlogger ()Static Method: use the name parameter we provide to retrieve existingLoggerObject. If this parameter does not exist in the frameworkLoggerObject, it will also createLoggerObject. The Code is as follows:
Log4net. ilog log = log4net. logmanager. getlogger ("logger-name ");
Generally (Class) Type (Type).Getlogger ()To track the classes that we are logging. Passed class(Class)Type(Type)AvailableTypeof (classname)Or use the following reflection method:
System. reflection. methodbase. getcurrentmethod (). declaringtype
Although the symbol is longer, the latter can be used for some occasions, such as obtaining the class of the call Method(Class)Type(Type).
ProgramIf it is used, record it. If there is anything wrong, please point it out!