侧边栏壁纸
博主头像
怪客のBlog 博主等级

行动起来,活在当下

  • 累计撰写 35 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
C#

C# 使用 log4net记录日志

怪客
2023-01-18 / 0 评论 / 1 点赞 / 219 阅读 / 0 字

安装依赖包

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&quot;.log&quot;" />
			<param name="StaticLogFileName" value="false" />
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern" value="%n
{
    &quot;system&quot;: &quot;DataScraping&quot;,
    &quot;datetime&quot;: &quot;%d&quot;,
    &quot;description&quot;: &quot;%m&quot;,
  &quot;level&quot;: &quot;%p&quot;,
    &quot;info&quot;: &quot;%exception&quot;
}" />
			</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&quot;.log&quot;" />
			<param name="StaticLogFileName" value="false" />
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern" value="%n
{
    &quot;system&quot;: &quot;DataScraping&quot;,
    &quot;datetime&quot;: &quot;%d&quot;,
    &quot;description&quot;: &quot;%m&quot;,
  &quot;level&quot;: &quot;%p&quot;,
    &quot;info&quot;: &quot;%exception&quot;
}" />
			</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&quot;.log&quot;" />
			<param name="StaticLogFileName" value="false" />
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern" value="%n
{
    &quot;system&quot;: &quot;DataScraping&quot;,
    &quot;datetime&quot;: &quot;%d&quot;,
    &quot;description&quot;: &quot;%m&quot;,
  &quot;level&quot;: &quot;%p&quot;,
    &quot;info&quot;: &quot;%exception&quot;
}" />
			</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&quot;.log&quot;" />
			<param name="StaticLogFileName" value="false" />
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern" value="%n
{
    &quot;system&quot;: &quot;DataScraping&quot;,
    &quot;datetime&quot;: &quot;%d&quot;,
    &quot;description&quot;: &quot;%m&quot;,
  &quot;level&quot;: &quot;%p&quot;,
    &quot;info&quot;: &quot;%exception&quot;
}" />
			</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>

修改文件属性
image-1674024918845

初始化

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");
        }

image-1674025019540
image-1674025381382

1

评论区