Microservice Framework Hands-on Building Tutorial 1: Adding Global Dependency Injection Log Log4Net

The log uses Log4Net to replace Microsoft's native log

The first step: nuget introduces two packages

insert image description here

Step 2: Add in Program

insert image description here

Step 3: Create log4net.config

Create an xml file named log4net.config

insert image description here
Add the following code inside

<?xml version="1.0" encoding="utf-8"?>
<log4net>
	<appender name="Log4Name" type="log4net.Appender.RollingFileAppender">
		<!--日志路径-->
		<param name="File" value="log\log.log" />
		<!--是否是向文件中追加日志-->
		<param name="AppendToFile" value="true" />
		<!--log保留个数-->
		<param name="MaxSizeRollBackups" value="200" />
		<!--单个日志文件大小-->
		<param name="MaximumFileSize" value="5MB" />
		<!--日志文件名是否是固定不变的-->
		<param name="StaticLogFileName" value="false" />
		<!--日志文件名格式为:2008-08-31.log-->
		<DatePattern value="yyyy-MM-dd HH'时.log'"></DatePattern>
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<!--按照何种方式产生多个日志文件(日期[Date],文件大小[Size],混合[Composite])-->
		<param name="RollingStyle" value="Size" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
		</layout>
	</appender>
	<root>
		<!--() OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL () -->
		<level value="ALL" />
		<appender-ref ref="Log4Name" />
	</root>
</log4net>

4. Adjust the default log filtering level

insert image description here
insert image description here
debug is the lowest level, so now all log types will be recorded

5. use

Just find a service that implements DogEggServiceInterface, there is no introduction in the first chapter, you can go to git to pull the source code

DogEggServiceInterface

Inject and use in the required class constructor

look at the log
insert image description here

I am a dog and I hope you are happy!

Guess you like

Origin blog.csdn.net/weixin_38083655/article/details/121873246