log4net Quick Start

Original Address: https: //www.cnblogs.com/lsgsanxiao/p/5845300.html

Slightly deletion


1. The configuration file can be created log4net.config separate file, and then manually specified directory, the following code may be inserted at the node configuration items of app.config or web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<logger name="logerror">
<level value="ERROR" />
<appender-ref ref="ErrorAppender" />
</logger>
<logger name= "the loginfo" > 
< Level value = "the INFO"  /> 
< the appender REF- REF = "InfoAppender"  /> 
</ Logger > 
< the appender name = "ErrorAppender" type = "log4net.Appender.RollingFileAppender" > 
<! - set the log storage path -> 
< param name = "file" value = "WebLog the Data // // // LogError"  /> 
<-! whether to append to the file -> 
< param name = "AppendToFile" value="true" />
<!--The largest number of log files generated, retained only the latest over the n. Setpoint value = "- 1" is the number of all File -> 
< param name = "MaxSizeRollBackups" value = "100"  /> 
< param name = "the MaxFileSize" value = "1024"  /> 
<-! Are only a written document -> 
< param name = "StaticLogFileName" value = "false"  /> 
<-! this is produced by date folder and also add the date before the file name -> 
< param name = "DatePattern" value = "YYYYMM / dd & quot; & quot .log;"  /> 
<!
< Param name = "RollingStyle" value = "a Date"  /> 
< layout type = "log4net.Layout.PatternLayout" > 
< param name = "ConversionPattern" value = "% n-time exception:% d [% t]% n Exception level:% - 5p% n unusual location: [% thread] (% file :% line)% n message description:% message% n exception:% exception% n-% n-"  /> 
</ layout > 
</ the appender > 
< the appender name = "InfoAppender" type = "log4net.Appender.RollingFileAppender" > 
<-! set the log storage path ->
<param name="File"value = "WebLog the Data // // // the LogInfo"  /> 
<-! whether to append to the file -> 
< param name = "AppendToFile" value = "to true"  /> 
<-! number of log files generated by most more than leaving only the latest of n. Setpoint value = "- 1" is the number of all File -> 
< param name = "MaxSizeRollBackups" value = "100"  /> 
< param name = "the MaxFileSize" value = "1024"  /> 
<-! Are only a written document -> 
< param name = "StaticLogFileName" 
-> 
< param name = "DatePattern" value = "YYYYMM / dd & quot; & quot .log;"  /> 
! <- generating a plurality of log files (Date [a Date] in accordance with the manner in which the file size [Size], mixed [ Composite]) -> 
< param name = "RollingStyle" value = "a Date"  /> 
< layout type = "log4net.Layout.PatternLayout" > 
< param name = "ConversionPattern" value = "% n-log time:% d [ % t]% n log level:% - 5p% n message description:% C [% X] n-%%% n-m "  /> 
</ layout > 
</ the appender >
</log4net>
</configuration>

 

2. Read configuration
adding the code to read the configuration of the start of the project file, it can be said Program.cs or Global.cs such as
under direct unloading if the project configuration file, read this way

log4net.Config.XmlConfigurator.Configure();

If a separate written log4net.config file, you need to make the file path passed to Configure () method

var fi = new System.IO.FileInfo(path);
log4net.Config.XmlConfigurator.Configure(fi);

3. Help class

///  <Summary>  
/// LogHelper the summary. 
///  </ Summary>  
public  class LogHelper {
 ///  <Summary> 
/// static readonly info entity object information
 ///  </ Summary> 
public  static  Readonly log4net.ILog loginfo = log4net.LogManager.GetLogger ( " the loginfo " );
 ///  <Summary> 
/// static readonly entity object information error
 ///  </ Summary> 
public  static  readonly log4net.ILog logError = log4net.LogManager.GetLogger ( " LogError " ); 

///  <Summary >
/// Add info information
 ///  </ Summary> 
///  <param name = "info"> Custom Log Description </ param> 
public  static  void WriteLog ( String info) {
 the try {
 IF (Loginfo.IsInfoEnabled) { 
Loginfo.Info (info); 
} 
} the catch {} 
} 


///  <Summary> 
/// Add abnormality information
 ///  </ Summary> 
///  <param name = "info"> custom log Description < / param> 
///  <param name = "EX"> abnormality information </ param>
public static void WriteLog(string info, Exception ex) {
try {
if (Logerror.IsErrorEnabled) {
Logerror.Error(info, ex);
}
} catch { }
}
}

 

Guess you like

Origin www.cnblogs.com/fb208/p/11327909.html
Recommended