要求:日志功能:info, debug ,error 三种级别的日志均可以通过log 日志的方式查看,支持日志按天分文件保存文件夹; 添加日志自动清除策略,支持可配置,默认保留30天
第一步:在NuGet中添加log4net
工具->NuGet包管理器->管理解决方案的NuGet程序包
第二步:增加配置文件log4net.config
<?xml version="1.0" encoding="utf-8" ?> <log4net> <root> <!--打印级别,如果要打Debug日志,请改为BEBUG--> <level value="INFO" /> <appender-ref ref="ConsoleOutput" /> <appender-ref ref="RollingFileDebug" /> <appender-ref ref="RollingFileInfo" /> <appender-ref ref="RollingFileError" /> </root> <appender name="ConsoleOutput" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%-5level] [%date{yyyy-MM-dd HH:mm:ss.fff}] %24logger [%line] [%t] [%M] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO"/> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> <appender name="RollingFileDebug" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="logs/debug/client_debug_log_"/> <appendToFile value="true"/> <encoding value="utf-8" /> <maxSizeRollBackups value="30"/> <maximumFileSize value="10MB"/> <staticLogFileName value="false"/> <datePattern value="yyyy-MM-dd".log""/> <rollingStyle value="Date"/> <!--保证在日志分割时,新日志的序号会添加在后缀名之前--> <preserveLogFileNameExtension value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%-5level] [%date{yyyy-MM-dd HH:mm:ss.fff}] %24logger [%line] [%t] [%M] - %message%newline" /> </layout> <lockingmodel type="log4net.appender.fileappender+minimallock" /> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="DEBUG" /> </filter> </appender> <appender name="RollingFileInfo" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="logs/info/client_info_log_"/> <appendToFile value="true"/> <encoding value="utf-8" /> <maxSizeRollBackups value="30"/> <maximumFileSize value="10MB"/> <staticLogFileName value="false"/> <datePattern value="yyyy-MM-dd".log""/> <rollingStyle value="Date"/> <!--保证在日志分割时,新日志的序号会添加在后缀名之前--> <preserveLogFileNameExtension value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%-5level] [%date{yyyy-MM-dd HH:mm:ss.fff}] %24logger [%line] [%t] [%M] - %message%newline" /> </layout> <lockingmodel type="log4net.appender.fileappender+minimallock" /> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="ERROR" /> </filter> </appender> <appender name="RollingFileError" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="logs/error/client_error_log_"/> <appendToFile value="true"/> <encoding value="utf-8" /> <maxSizeRollBackups value="30"/> <maximumFileSize value="10MB"/> <staticLogFileName value="false"/> <datePattern value="yyyy-MM-dd".log""/> <rollingStyle value="Date"/> <!--保证在日志分割时,新日志的序号会添加在后缀名之前--> <preserveLogFileNameExtension value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%-5level] [%date{yyyy-MM-dd HH:mm:ss.fff}] %24logger [%line] [%t] [%M] - %message%newline" /> </layout> <lockingmodel type="log4net.appender.fileappender+minimallock" /> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> </log4net>
第三步:在Main中增加一行代码(用于从指定的XML配置文件(在这个例子中是log4net.config
)中加载日志配置)
XmlConfigurator.Configure(new FileInfo("log4net.config"));
标签:log4net,配置文件,C#,步骤,NuGet,日志,config From: https://www.cnblogs.com/guobm/p/18034092