.NET6 WebApi使用log4net

网友投稿 969 2022-06-06


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\目录下

到此这篇关于.NET6在WebApi中使用日志组件log4net的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。



版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:.NET6实现动态API(.net动态网页)
下一篇:C#  Fluent API
相关文章

 发表评论

暂时没有评论,来抢沙发吧~