Log4net 根据日志类别保存到不同的文件,并按照日期生成不同文件名称
<configuration> <configSections> <!--日志记录--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <root></root> <logger name="Logger"> <level value="ALL" /> <appender-ref ref="ErrorRollingFileAppender" /> <appender-ref ref="InfoRollingFileAppender" /> </logger> <!--数据日志--> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.'Info.txt'" /> <param name="staticLogFileName" value="false"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <!--异常日志--> <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\" /> <param name="AppendToFile" value="true" /> <param name="rollingStyle" value="Date" /> <param name="datePattern" value="yyyy-MM-dd.'Error.txt'" /> <param name="staticLogFileName" value="false"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> </log4net> </configuration>
代码使用记录器:
private static readonly ILog infoLogger = LogManager.GetLogger("Logger"); XmlConfigurator.Configure(); infoLogger.Info(message);