当前位置:   article > 正文

VisualStudio C#中使用Log4net输出不同类型的日志

VisualStudio C#中使用Log4net输出不同类型的日志

20240514

By wdhuag


目录

前言:

参考:

安装Log4net包并引用:

创建log4net.config配置文件,属性要设置始终复制:

在AssemblyInfo.cs中加入配置:

 初始化时读取配置:

调用:

一个简单的例子:


前言:

Log4net输出不同类型的日志是基本操作,但是网上的举例注释太杂、实例太乱,这里做一个简化的例子。


参考:

Apache log4net – Apache log4net Manual: Configuration - Apache log4net

https://www.cnblogs.com/boise/p/18002739

https://www.cnblogs.com/amylis_chen/p/6069487.html

log4net各种Filter使用_log4net.filter.denyallfilter-CSDN博客

https://www.cnblogs.com/cdaq/p/5052055.html

https://www.cnblogs.com/nanfei/p/6542275.html


安装Log4net包并引用:


创建log4net.config配置文件,属性要设置始终复制:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  5. </configSections>
  6. <!--站点日志配置部分-->
  7. <log4net>
  8. <!--root节点,只能有一个,是所有logger对象的父节点-->
  9. <root>
  10. <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
  11. <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
  12. <!--如果没有定义LEVEL的值,则缺省为DEBUG-->
  13. <level value="All" />
  14. <!--Appenders节点,定义日志的输出方式-->
  15. <appender-ref ref="InfoAppender" />
  16. <appender-ref ref="WarnAppender" />
  17. <appender-ref ref="ErrorAppender" />
  18. <appender-ref ref="DebugAppender" />
  19. <appender-ref ref="FatalAppender" />
  20. </root>
  21. <!--自定义logger节点-->
  22. <logger name="StationLog">
  23. <level value="ALL" />
  24. <appender-ref ref="StationAppender" />
  25. <!--additivity用于设置是否继承root节点,为false时root的appender执行后不会再执行logger的appender-->
  26. <additivity value="true"/>
  27. </logger>
  28. <logger name="SnLog">
  29. <level value="ALL" />
  30. <appender-ref ref="SnAppender" />
  31. <additivity value="true"/>
  32. </logger>
  33. <logger name="MesLog">
  34. <level value="ALL" />
  35. <appender-ref ref="MesAppender" />
  36. <additivity value="true"/>
  37. </logger>
  38. <!--RollingFileAppender将日志以回滚文件的形式写到文件中-->
  39. <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
  40. <!--当RollingStyle为Composite或Date,这里设置文件名格式-->
  41. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  42. <!--多线程时采用最小锁定-->
  43. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  44. <!--文本编码-->
  45. <encoding value="utf-8" />
  46. <!--文件路径,运行文件目录.\ 上一层..\-->
  47. <file type="log4net.Util.PatternString" value =".\日志\INFO\"/>
  48. <!--当文件存在时,是否在原文件上追加内容-->
  49. <appendToFile value="true" />
  50. <!--最大文件大小(可以使用KB,MB,GB为单位,默认为字节)-->
  51. <param name="MaxFileSize" value="10240"/>
  52. <!-- 创建新文件的方式-->
  53. <rollingStyle value="Date" />
  54. <!--staticLogFileName为true时,RollingStyler的Date值将无效,且需要在file里指定文件名,所有日志都会记录在这个文件里-->
  55. <staticLogFileName value="false" />
  56. <!--最大行数-->
  57. <param name="MaxSizeRollBackups" value="100" />
  58. <!--日志输出格式,%m(message信息),%d{yyy-MM-dd HH:mm:ss }(datetime日期和格式),%r(runtime运行时间),%p(priority日志等级),%c(class类名),%t(thread线程id),%l(line行号),%f(file文件名)-->
  59. <layout type="log4net.Layout.PatternLayout">
  60. <conversionPattern value="%m" />
  61. </layout>
  62. <!--日志等级过滤器,value参数大写-->
  63. <filter type="log4net.Filter.LeveRangeFilter">
  64. <param name="levelMin" value="INFO"/>
  65. <param name="levelMax" value="INFO"/>
  66. </filter>
  67. <!--输出日志信息中包含特殊字符串才输出,AcceptOnMatch条件满足是否输出打印-->
  68. <filter type="log4net.Filter.StringMatchFilter">
  69. <param name="StringToMatch" value="Info"/>
  70. <param name="AcceptOnMatch" value="true"/>
  71. </filter>
  72. <!--Logger名称匹配才记录-->
  73. <filter type="log4net.Filter.LoggerMatchFilter">
  74. <loggerToMatch value="" />
  75. </filter>
  76. <!--输出日志信息中包含特殊字符串才输出,不设置这个就无法过滤-->
  77. <filter type="log4net.Filter.DenyAllFilter" />
  78. </appender>
  79. <appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
  80. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  81. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  82. <encoding value="utf-8" />
  83. <file type="log4net.Util.PatternString" value =".\日志\INFO\"/>
  84. <appendToFile value="true" />
  85. <param name="MaxFileSize" value="10240"/>
  86. <rollingStyle value="Date" />
  87. <staticLogFileName value="false" />
  88. <param name="MaxSizeRollBackups" value="100" />
  89. <layout type="log4net.Layout.PatternLayout">
  90. <conversionPattern value="%m" />
  91. </layout>
  92. <filter type="log4net.Filter.LeveRangeFilter">
  93. <param name="levelMin" value="INFO"/>
  94. <param name="levelMax" value="INFO"/>
  95. </filter>
  96. <filter type="log4net.Filter.StringMatchFilter">
  97. <param name="StringToMatch" value="Info"/>
  98. <param name="AcceptOnMatch" value="true"/>
  99. </filter>
  100. <filter type="log4net.Filter.LoggerMatchFilter">
  101. <loggerToMatch value="" />
  102. </filter>
  103. <filter type="log4net.Filter.DenyAllFilter" />
  104. </appender>
  105. <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
  106. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  107. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  108. <encoding value="utf-8" />
  109. <file type="log4net.Util.PatternString" value =".\日志\ERROR\"/>
  110. <appendToFile value="true" />
  111. <param name="MaxFileSize" value="10240"/>
  112. <rollingStyle value="Date" />
  113. <staticLogFileName value="false" />
  114. <param name="MaxSizeRollBackups" value="100" />
  115. <layout type="log4net.Layout.PatternLayout">
  116. <conversionPattern value="%m" />
  117. </layout>
  118. <filter type="log4net.Filter.LeveRangeFilter">
  119. <param name="levelMin" value="ERROR"/>
  120. <param name="levelMax" value="ERROR"/>
  121. </filter>
  122. <filter type="log4net.Filter.StringMatchFilter">
  123. <param name="StringToMatch" value="Error"/>
  124. <param name="AcceptOnMatch" value="true"/>
  125. </filter>
  126. <filter type="log4net.Filter.LoggerMatchFilter">
  127. <loggerToMatch value="" />
  128. </filter>
  129. <filter type="log4net.Filter.DenyAllFilter" />
  130. </appender>
  131. <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
  132. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  133. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  134. <encoding value="utf-8" />
  135. <file type="log4net.Util.PatternString" value =".\日志\DEBUG\"/>
  136. <appendToFile value="true" />
  137. <param name="MaxFileSize" value="10240"/>
  138. <rollingStyle value="Date" />
  139. <staticLogFileName value="false" />
  140. <param name="MaxSizeRollBackups" value="100" />
  141. <layout type="log4net.Layout.PatternLayout">
  142. <conversionPattern value="%m" />
  143. </layout>
  144. <filter type="log4net.Filter.LeveRangeFilter">
  145. <param name="levelMin" value="DEBUG"/>
  146. <param name="levelMax" value="DEBUG"/>
  147. </filter>
  148. <filter type="log4net.Filter.StringMatchFilter">
  149. <param name="StringToMatch" value="Debug"/>
  150. <param name="AcceptOnMatch" value="true"/>
  151. </filter>
  152. <filter type="log4net.Filter.LoggerMatchFilter">
  153. <loggerToMatch value="" />
  154. </filter>
  155. <filter type="log4net.Filter.DenyAllFilter" />
  156. </appender>
  157. <appender name="FatalAppender" type="log4net.Appender.RollingFileAppender">
  158. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  159. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  160. <encoding value="utf-8" />
  161. <file type="log4net.Util.PatternString" value =".\日志\FATAL\"/>
  162. <appendToFile value="true" />
  163. <param name="MaxFileSize" value="10240"/>
  164. <rollingStyle value="Date" />
  165. <staticLogFileName value="false" />
  166. <param name="MaxSizeRollBackups" value="100" />
  167. <layout type="log4net.Layout.PatternLayout">
  168. <conversionPattern value="%m" />
  169. </layout>
  170. <filter type="log4net.Filter.LeveRangeFilter">
  171. <param name="levelMin" value="FATAL"/>
  172. <param name="levelMax" value="FATAL"/>
  173. </filter>
  174. <filter type="log4net.Filter.StringMatchFilter">
  175. <param name="StringToMatch" value="Fatal"/>
  176. <param name="AcceptOnMatch" value="true"/>
  177. </filter>
  178. <filter type="log4net.Filter.LoggerMatchFilter">
  179. <loggerToMatch value="" />
  180. </filter>
  181. <filter type="log4net.Filter.DenyAllFilter" />
  182. </appender>
  183. <appender name="StationAppender" type="log4net.Appender.RollingFileAppender">
  184. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  185. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  186. <encoding value="utf-8" />
  187. <file type="log4net.Util.PatternString" value =".\日志\Station\"/>
  188. <appendToFile value="true" />
  189. <param name="MaxFileSize" value="10240"/>
  190. <rollingStyle value="Date" />
  191. <staticLogFileName value="false" />
  192. <param name="MaxSizeRollBackups" value="100" />
  193. <layout type="log4net.Layout.PatternLayout">
  194. <conversionPattern value="%m" />
  195. </layout>
  196. <!--filter type="log4net.Filter.LeveRangeFilter">
  197. <param name="levelMin" value="INFO"/>
  198. <param name="levelMax" value="INFO"/>
  199. </filter-->
  200. <!--filter type="log4net.Filter.StringMatchFilter">
  201. <param name="StringToMatch" value="Info"/>
  202. <param name="AcceptOnMatch" value="true"/>
  203. </filter-->
  204. <filter type="log4net.Filter.LoggerMatchFilter">
  205. <loggerToMatch value="StationLog" />
  206. </filter>
  207. <filter type="log4net.Filter.DenyAllFilter" />
  208. </appender>
  209. <appender name="SnAppender" type="log4net.Appender.RollingFileAppender">
  210. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  211. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  212. <encoding value="utf-8" />
  213. <file type="log4net.Util.PatternString" value =".\日志\SN\"/>
  214. <appendToFile value="true" />
  215. <param name="MaxFileSize" value="10240"/>
  216. <rollingStyle value="Date" />
  217. <staticLogFileName value="false" />
  218. <param name="MaxSizeRollBackups" value="100" />
  219. <layout type="log4net.Layout.PatternLayout">
  220. <conversionPattern value="%m" />
  221. </layout>
  222. <!--filter type="log4net.Filter.LeveRangeFilter">
  223. <param name="levelMin" value="INFO"/>
  224. <param name="levelMax" value="INFO"/>
  225. </filter-->
  226. <!--filter type="log4net.Filter.StringMatchFilter">
  227. <param name="StringToMatch" value="Info"/>
  228. <param name="AcceptOnMatch" value="true"/>
  229. </filter-->
  230. <filter type="log4net.Filter.LoggerMatchFilter">
  231. <loggerToMatch value="SnLog" />
  232. </filter>
  233. <filter type="log4net.Filter.DenyAllFilter" />
  234. </appender>
  235. <appender name="MesAppender" type="log4net.Appender.RollingFileAppender">
  236. <datePattern value="yyyyMM\\yyyyMMdd'.log'" />
  237. <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  238. <encoding value="utf-8" />
  239. <file type="log4net.Util.PatternString" value =".\日志\MES\"/>
  240. <appendToFile value="true" />
  241. <param name="MaxFileSize" value="10240"/>
  242. <rollingStyle value="Date" />
  243. <staticLogFileName value="false" />
  244. <param name="MaxSizeRollBackups" value="100" />
  245. <layout type="log4net.Layout.PatternLayout">
  246. <conversionPattern value="%m" />
  247. </layout>
  248. <!--filter type="log4net.Filter.LeveRangeFilter">
  249. <param name="levelMin" value="INFO"/>
  250. <param name="levelMax" value="INFO"/>
  251. </filter-->
  252. <!--filter type="log4net.Filter.StringMatchFilter">
  253. <param name="StringToMatch" value="Info"/>
  254. <param name="AcceptOnMatch" value="true"/>
  255. </filter-->
  256. <filter type="log4net.Filter.LoggerMatchFilter">
  257. <loggerToMatch value="MesLog" />
  258. </filter>
  259. <filter type="log4net.Filter.DenyAllFilter" />
  260. </appender>
  261. </log4net>
  262. </configuration>


在AssemblyInfo.cs中加入配置:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]


 初始化时读取配置:

  1. //读取Log4net配置文件,默认选择root节点
  2. private static readonly ILog _RootLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  3. //private static readonly ILog _RootLog = LogManager.GetLogger("root");
  4. //选择自定义节点
  5. private static readonly ILog _StationLog = LogManager.GetLogger("StationLog");
  6. private static readonly ILog _SnLog = LogManager.GetLogger("SnLog");
  7. private static readonly ILog _MesLog = LogManager.GetLogger("MesLog");


调用:

  1. _RootLog.Info(text);
  2. _RootLog.Warn(text);
  3. _RootLog.Error(text);
  4. _RootLog.Debug(text);
  5. _RootLog.Fatal(text);
  6. _StationLog.Info(text);
  7. _SnLog.Info(text);
  8. _MesLog.Info(text);


一个简单的例子:

度盘,提取码:mbj1 


END

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/571903
推荐阅读
相关标签
  

闽ICP备14008679号