欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

.NET6在WebApi中使用日志组件log4net

时间:2022-03-29 09:13:43|栏目:.NET代码|点击:

1、安装依赖

Microsoft.Extensions.Logging.Log4Net.AspNetCore

2、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<!-- This section contains the log4net configuration settings -->
	<log4net>
		<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
			<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
		</appender>

		<appender name="FileAppender" type="log4net.Appender.FileAppender">
			<file value="log-file.log" />
			<appendToFile value="true" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
			</layout>
		</appender>

		<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
			<file value="LogFile/Error/" />
			<appendToFile value="true" />
			<rollingStyle value="Composite" />
			<staticLogFileName value="false" />
			<datePattern value="yyyyMMdd'.log'" />
			<maxSizeRollBackups value="10" />
			<maximumFileSize value="1MB" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
			</layout>
			<filter type="log4net.Filter.LevelRangeFilter">
				<levelMin value="ERROR" />
				<!--配置该节点会监听的日志错误级别-->
				<levelMax value="FATAL" />
			</filter>
		</appender>

		<appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
			<file value="LogFile/Warn/" />
			<appendToFile value="true" />
			<rollingStyle value="Date"/>
			<datePattern value="yyyy-MM-dd'.log'"/>
			<maxSizeRollBackups value="100" />
			<staticLogFileName value="false" />
			<encoding value="utf-8" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%newline%date [%thread %-5level] %n -- %m%n" />
			</layout>
			<filter type="log4net.Filter.LevelRangeFilter">
				<levelMin value="WARN" />
				<levelMax value="WARN" />
			</filter>
		</appender>
		
		<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
			<file value="LogFile/Info/" />
			<appendToFile value="true" />
			<rollingStyle value="Date"/>
			<datePattern value="yyyy-MM-dd'.log'"/>
			<maxSizeRollBackups value="100" />
			<staticLogFileName value="false" />
			<encoding value="utf-8" />
			<layout type="log4net.Layout.PatternLayout">
				<!--<conversionPattern value="%newline%date [%thread %-5level] %n %m%n" />-->
				<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
			</layout>
			<filter type="log4net.Filter.LevelRangeFilter">
				<levelMin value="TRACE " />
				<levelMax value="INFO" />
			</filter>
		</appender>

		<!-- Setup the root category, add the appenders and set the default level -->
		<root>
			<level value="ALL" />
			<appender-ref ref="ConsoleAppender" />
			<appender-ref ref="FileAppender" />
			<appender-ref ref="RollingLogFileAppender" />
			<appender-ref ref="WarnRollingFileAppender" />
			<appender-ref ref="InfoRollingFileAppender" />
		</root>

	</log4net>
</configuration>

3、注册组件

Program.cs文件中

ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));

4、使用

/// <summary>
/// 日志
/// </summary>
public ILog log = LogManager.GetLogger("NETCoreRepository", this.GetType());

文件生成在项目bin\Debug\net6.0\LogFile\目录下

上一篇:提示出现unresolved external symbol _main的解决方法

栏    目:.NET代码

下一篇:HTML文本框的值改变后触发后台代码的方法

本文标题:.NET6在WebApi中使用日志组件log4net

本文地址:http://www.codeinn.net/misctech/197543.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有