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配置文件,属性要设置始终复制:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--站点日志配置部分-->
<log4net>
<!--root节点,只能有一个,是所有logger对象的父节点-->
<root>
<!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
<!--如果没有定义LEVEL的值,则缺省为DEBUG-->
<level value="All" />
<!--Appenders节点,定义日志的输出方式-->
<appender-ref ref="InfoAppender" />
<appender-ref ref="WarnAppender" />
<appender-ref ref="ErrorAppender" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="FatalAppender" />
</root>
<!--自定义logger节点-->
<logger name="StationLog">
<level value="ALL" />
<appender-ref ref="StationAppender" />
<!--additivity用于设置是否继承root节点,为false时root的appender执行后不会再执行logger的appender-->
<additivity value="true"/>
</logger>
<logger name="SnLog">
<level value="ALL" />
<appender-ref ref="SnAppender" />
<additivity value="true"/>
</logger>
<logger name="MesLog">
<level value="ALL" />
<appender-ref ref="MesAppender" />
<additivity value="true"/>
</logger>
<!--RollingFileAppender将日志以回滚文件的形式写到文件中-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<!--当RollingStyle为Composite或Date,这里设置文件名格式-->
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<!--多线程时采用最小锁定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<!--文本编码-->
<encoding value="utf-8" />
<!--文件路径,运行文件目录.\ 上一层..\-->
<file type="log4net.Util.PatternString" value =".\日志\INFO\"/>
<!--当文件存在时,是否在原文件上追加内容-->
<appendToFile value="true" />
<!--最大文件大小(可以使用KB,MB,GB为单位,默认为字节)-->
<param name="MaxFileSize" value="10240"/>
<!-- 创建新文件的方式-->
<rollingStyle value="Date" />
<!--staticLogFileName为true时,RollingStyler的Date值将无效,且需要在file里指定文件名,所有日志都会记录在这个文件里-->
<staticLogFileName value="false" />
<!--最大行数-->
<param name="MaxSizeRollBackups" value="100" />
<!--日志输出格式,%m(message信息),%d{yyy-MM-dd HH:mm:ss }(datetime日期和格式),%r(runtime运行时间),%p(priority日志等级),%c(class类名),%t(thread线程id),%l(line行号),%f(file文件名)-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<!--日志等级过滤器,value参数大写-->
<filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="INFO"/>
</filter>
<!--输出日志信息中包含特殊字符串才输出,AcceptOnMatch条件满足是否输出打印-->
<filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Info"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<!--Logger名称匹配才记录-->
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="" />
</filter>
<!--输出日志信息中包含特殊字符串才输出,不设置这个就无法过滤-->
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\INFO\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="INFO"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Info"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\ERROR\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="ERROR"/>
<param name="levelMax" value="ERROR"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Error"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\DEBUG\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="DEBUG"/>
<param name="levelMax" value="DEBUG"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Debug"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="FatalAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\FATAL\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="FATAL"/>
<param name="levelMax" value="FATAL"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Fatal"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="StationAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\Station\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<!--filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="INFO"/>
</filter-->
<!--filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Info"/>
<param name="AcceptOnMatch" value="true"/>
</filter-->
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="StationLog" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="SnAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\SN\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<!--filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="INFO"/>
</filter-->
<!--filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Info"/>
<param name="AcceptOnMatch" value="true"/>
</filter-->
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="SnLog" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="MesAppender" type="log4net.Appender.RollingFileAppender">
<datePattern value="yyyyMM\\yyyyMMdd'.log'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<file type="log4net.Util.PatternString" value =".\日志\MES\"/>
<appendToFile value="true" />
<param name="MaxFileSize" value="10240"/>
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
<!--filter type="log4net.Filter.LeveRangeFilter">
<param name="levelMin" value="INFO"/>
<param name="levelMax" value="INFO"/>
</filter-->
<!--filter type="log4net.Filter.StringMatchFilter">
<param name="StringToMatch" value="Info"/>
<param name="AcceptOnMatch" value="true"/>
</filter-->
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="MesLog" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
</log4net>
</configuration>
在AssemblyInfo.cs中加入配置:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
初始化时读取配置:
//读取Log4net配置文件,默认选择root节点
private static readonly ILog _RootLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
//private static readonly ILog _RootLog = LogManager.GetLogger("root");
//选择自定义节点
private static readonly ILog _StationLog = LogManager.GetLogger("StationLog");
private static readonly ILog _SnLog = LogManager.GetLogger("SnLog");
private static readonly ILog _MesLog = LogManager.GetLogger("MesLog");
调用:
_RootLog.Info(text);
_RootLog.Warn(text);
_RootLog.Error(text);
_RootLog.Debug(text);
_RootLog.Fatal(text);
_StationLog.Info(text);
_SnLog.Info(text);
_MesLog.Info(text);
一个简单的例子:
度盘,提取码:mbj1
END