第一步:添加app.log4net配置文件
<? xml version="1.0" encoding="utf-8" ?> < configuration > < configSections > < section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </ configSections > < log4net > < root > < level value ="ALL" /> < appender-ref ref ="rollingFile" /> </ root > < appender name ="rollingFile" type ="log4net.Appender.RollingFileAppender,log4net" > < param name ="File" value ="log\\log.txt" /> < param name ="AppendToFile" value ="false" /> < param name ="RollingStyle" value ="Date" /> < param name ="DatePattern" value ="yyyy.MM.dd" /> < param name ="StaticLogFileName" value ="true" /> < layout type ="log4net.Layout.PatternLayout,log4net" > < param name ="ConversionPattern" value ="%d [%t] %-5p %c - %m%n" /> < param name ="Header" value =" ----------------------header-------------------------- " /> < param name ="Footer" value =" ----------------------footer-------------------------- " /> </ layout > </ appender > </ log4net > </ configuration >
第二步:启用配置文件,在AssemblyInfo.cs文件里面添加配置
[ assembly: log4net.Config.XmlConfigurator(ConfigFile = " ba.log4net " , Watch = true)]
第三步:使用Log4net记录日志
1、获取日志对象
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
2、写日志
log.Info( " 生成成功 " );