<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- This section contains the log4net configuration settings --> <log4net> <!-- Setup the root category, add the appenders and set the default level --> <!--控制级别,由低到高:ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF--> <root> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--日志文件路径--> <file value="log//" /> <!--是否是向文件中追加日志--> <appendToFile value="true" /> <!--日志根据日期滚动--> <rollingStyle value="Date" /> <!--日志文件名是否是固定不变的--> <staticLogFileName value="false" /> <!--日志文件名格式为:2008-08-31.log--> <datePattern value="yyyy-MM-dd'.log'" /> <!--log保留天数--> <maxSizeRollBackups value="10" /> <!--最大文件大小--> <maximumFileSize value="1MB" /> <!--信息日志布局--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net> </configuration>log4net.config
初始化
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo( "C:\\log4net.config"));
实例化对象
static readonly ILog log = LogManager.GetLogger(typeof(MainService));