config配置文件如下:
<?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] %-5p %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] %-5p %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>
<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] %-5p %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] %-5p %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>
可以增加多个logger节和appender节配合使用
<level value="ALL"/> 这句话意思是针对Warn、Debug、Info所有级别的提示都使用下面的介质
服务器端代码如下:
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 log = 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----");
}
}
}
{
/// <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 log = 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----");
}
}
}
Log4net框架定义了一个叫做LogManager的类,用来管理所有的logger对象。它有一个GetLogger()静态方法,用我们提供的名字参数来检索已经存在的Logger对象。如果框架里不存在该Logger对象,它也会为我们创建一个Logger对象。代码如下所示:
log4net.ILog log = log4net.LogManager.GetLogger("logger-name");
通常来说,我们会以类(class)的类型(type)为参数来调用GetLogger(),以便跟踪我们正在进行日志记录的类。传递的类(class)的类型(type)可以用typeof(Classname)方法来获得,或者可以用如下的反射方法来获得:
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
尽管符号长了一些,但是后者可以用于一些场合,比如获取调用方法的类(class)的类型(type)。
程序中用到了就记录下来,如有什么不对的地方请指出!