Log4net
log4net比较流行的一个日志框架
1、程序信息配置里面AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
2、配置app.config
配置节点configSections
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler log4net"/> </configSections>
配置的根节点configuration内
<log4net> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:PROD_LogsApp_LogsLogsAppALL\%date{yyyy-MM-dd}all_%date{yyyy-MM-dd}.log" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy-MM-dd/all_yyyy-MM-dd".log"" /> <staticLogFileName value="true" /> <maximumFileSize value="10MB" /> <maxSizeRollBackups value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="TestRollingFile" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:PROD_LogsApp_LogsLogsAppTestALL\%date{yyyy-MM-dd}all_%date{yyyy-MM-dd}.log" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy-MM-dd/all_yyyy-MM-dd".log"" /> <staticLogFileName value="true" /> <maximumFileSize value="10MB" /> <maxSizeRollBackups value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--error--> <appender name="ErrorRollingFile" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:PROD_LogsApp_LogsLogsApperror\%date{yyyy-MM-dd}error_%date{yyyy-MM-dd}.log" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyy-MM-dd/error_yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <maxSizeRollBackups value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelMatchFilter"> <param name="LevelToMatch" value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> <logger name="LogsApp.Test"> <level value="All" /> <appender-ref ref="TestRollingFile" /> </logger>
持久服务,日期问题
如果持续性长时间占用的服务类日志,上面配置需要做一定修改
<file type="log4net.Util.PatternString" value="D:PROD_LogsApp_LogsLogsAppTest" /> <datePattern value="yyyy-MM-dd/all_yyyy-MM-dd".log"" /> <staticLogFileName value="false" />
上面rollingStyle如果不加则按日期也按文件大小分割日志
或者:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
然后把上面的两个配置放入单独的文件中
资料:
https://blog.csdn.net/wyy_hnu/article/details/11469797
https://www.cnblogs.com/lvdongjie/p/6122013.html