安装依赖包
Install-Package log4net
添加配置文件
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net debug="false">
<appender name="info" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log/info/" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="-1"/>
<param name="MaximumFileSize" value="5MB"/>
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd_HH".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n
{
"system": "DataScraping",
"datetime": "%d",
"description": "%m",
"level": "%p",
"info": "%exception"
}" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="error" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log/error/" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="-1"/>
<param name="MaximumFileSize" value="5MB"/>
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd_HH".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n
{
"system": "DataScraping",
"datetime": "%d",
"description": "%m",
"level": "%p",
"info": "%exception"
}" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="debug" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log/debug/" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="-1"/>
<param name="MaximumFileSize" value="5MB"/>
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd_HH".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n
{
"system": "DataScraping",
"datetime": "%d",
"description": "%m",
"level": "%p",
"info": "%exception"
}" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
</appender>
<appender name="warn" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log/warn/" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="-1"/>
<param name="MaximumFileSize" value="5MB"/>
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd_HH".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%n
{
"system": "DataScraping",
"datetime": "%d",
"description": "%m",
"level": "%p",
"info": "%exception"
}" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="WARN" />
</filter>
</appender>
<root>
<level value="ALL"></level>
<appender-ref ref="info"/>
<appender-ref ref="error"/>
<appender-ref ref="debug"/>
<appender-ref ref="warn"/>
<appender-ref ref="ConsoleAppender"></appender-ref>
</root>
<!-- 将日志输出到控制台 -->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
修改文件属性
初始化
ILoggerRepository repository = LogManager.GetRepository(Assembly.GetEntryAssembly());
BasicConfigurator.Configure(repository);
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
ILog log=LogManager.GetLogger(repository.Name, "MyApplication");
测试
static void Main(string[] args)
{
ILoggerRepository repository = LogManager.GetRepository(Assembly.GetEntryAssembly());
BasicConfigurator.Configure(repository);
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
ILog log = LogManager.GetLogger(repository.Name, "MyApplication");
log.Info("this is info message");
log.Debug("this is debug message");
log.Error("this is error message");
log.Warn("this is warn message");
}
评论区