How to implement different functions in a project into different log files [log4net]

Source: Internet
Author: User
Tags log4net

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.