log4net按日志级别(debug,info,warn,error,fatal)生成日志目录,同时每小时生成一个日志文件
配置文件:
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <root><!-- 根logger,所有其它logger都默认继承它--> <level value="WARN" /><!-- 只有在这个级别或之上的事件才会被记录--> <appender-ref ref="LogFileAppender" /><!--要引用的appender的名字--> <appender-ref ref="ConsoleAppender" /> </root> <logger name="testApp.Logging"> <level value="DEBUG"/> </logger> <!--定义日志的输出方式--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > <param name="File" value="Log\\" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <datePattern value="yyyyMMdd" /> <param name="DatePattern" value="yyyyMMdd\\yyyyMMdd‘_logtest.log‘" /> <param name="StaticLogFileName" value="false" /> <RollingStyle value="Date" /> <layout type="log4net.Layout.PatternLayout"><!--定义appender使用的输出格式--> <param name="Header" value="[Header] "/> <param name="Footer" value="[Footer] "/> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"><!--定义此app使用的过滤器--> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="WARN" /> </filter> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" /> </layout> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Log4Net.console { class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); log4net.ILog log = log4net.LogManager.GetLogger("testApp.Logging");//获取一个日志记录器 log.Info(DateTime.Now.ToString() + ": login success");//写入一条新log Console.ReadLine(); } } }
时间: 2024-10-16 17:28:18