.NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库

一、效果

  1. 记录日志为文档
    .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库_第1张图片

  2. 记录日志到数据库

.NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库_第2张图片

二、添加NuGet包

.NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库_第3张图片

三、log4net.config代码配置


<log4net>
	
	<appender name="RollingFileDebug" type="log4net.Appender.RollingFileAppender" >
		
		<file value="logs\\" />
		
		<datePattern value="yyyy-MM-dd/'Debug.log'"/>
		
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="100" />
		<maximumFileSize value="3MB" />
		
		<rollingStyle value="Date"/>
		
		<staticLogFileName value="false" />
		
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%newline%newline【日志时间】: %d  【线程ID】: %thread    %newline【日志级别】: %-5p  %newline【日志对象】:%logger  %newline【日志内容】:%m %newline" />
		layout>
		<filter type="log4net.Filter.LevelRangeFilter">
			<param name="LevelMin" value="Debug" />
			<param name="LevelMax" value="Debug" />
		filter>
	appender>

	
	<appender name="RollingFileInfo" type="log4net.Appender.RollingFileAppender" >
		
		<file value="logs\\" />
		
		<datePattern value="yyyy-MM-dd/'Info.log'"/>
		
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="100" />
		<maximumFileSize value="3MB" />
		
		<rollingStyle value="Date"/>
		
		<staticLogFileName value="false" />
		
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%newline%newline【日志时间】:%d  【线程ID】:%thread    %newline【日志级别】:%-5p  %newline【日志对象】:%logger  %newline【日志内容】:%m %newline" />
		layout>
		<filter type="log4net.Filter.LevelRangeFilter">
			<param name="LevelMin" value="Info" />
			<param name="LevelMax" value="Info" />
		filter>
	appender>

	
	<appender name="RollingFileWarn" type="log4net.Appender.RollingFileAppender" >
		
		<file value="logs\\" />
		
		<datePattern value="yyyy-MM-dd/'Warn.log'"/>
		
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="100" />
		<maximumFileSize value="3MB" />
		
		<rollingStyle value="Date"/>
		
		<staticLogFileName value="false" />
		
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%newline%newline【日志时间】:%d  【线程ID】:%thread    %newline【日志级别】:%-5p  %newline【日志对象】:%logger  %newline【日志内容】:%m %newline" />
		layout>
		<filter type="log4net.Filter.LevelRangeFilter">
			<param name="LevelMin" value="Warn" />
			<param name="LevelMax" value="Warn" />
		filter>
	appender>

	
	<appender name="RollingFileError" type="log4net.Appender.RollingFileAppender" >
		
		<file value="logs\\" />
		
		<datePattern value="yyyy-MM-dd/'Error.log'"/>
		
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="100" />
		<maximumFileSize value="3MB" />
		
		<rollingStyle value="Date"/>
		
		<staticLogFileName value="false" />
		
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%newline%newline【日志时间】:%d  【线程ID】:%thread    %newline【日志级别】:%-5p  %newline【日志对象】:%logger  %newline【日志内容】:%m %newline" />
		layout>
		<filter type="log4net.Filter.LevelRangeFilter">
			<param name="LevelMin" value="Error" />
			<param name="LevelMax" value="Error" />
		filter>

	appender>

	
	<appender name="RollingFileFatal" type="log4net.Appender.RollingFileAppender" >
		
		<file value="logs\\" />
		
		<datePattern value="yyyy-MM-dd/'Fatal.log'"/>
		
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="100" />
		<maximumFileSize value="3MB" />
		
		<rollingStyle value="Date"/>
		
		<staticLogFileName value="false" />
		
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%newline%newline【日志时间】:%d  【线程ID】:%thread    %newline【日志级别】:%-5p  %newline【日志对象】:%logger  %newline【日志内容】:%m%newline" />
		layout>
		<filter type="log4net.Filter.LevelRangeFilter">
			<param name="LevelMin" value="Fatal" />
			<param name="LevelMax" value="Fatal" />
		filter>
	appender>

	
	<appender name="logAppender_SqlServer" type="log4net.Appender.ADONetAppender">
		<bufferSize value="0"/>
		<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
		
			<connectionString value="Data Source=xxxxxxxx;Initial Catalog=xxxxxxxx;User ID=sa;Password=********;"/>
			<commandText value="INSERT INTO LogData ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"/>
			<parameter>
				<parameterName value="@log_date"/>
				<dbType value="DateTime"/>
				<layout type="log4net.Layout.RawTimeStampLayout"/>
			parameter>
			<parameter>
				<parameterName value="@thread"/>
				<dbType value="String"/>
				<size value="50"/>
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%thread"/>
				layout>
			parameter>
			<parameter>
				<parameterName value="@log_level"/>
				<dbType value="String"/>
				<size value="50"/>
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%level"/>
				layout>
			parameter>
			<parameter>
				<parameterName value="@logger"/>
				<dbType value="String"/>
				<size value="200"/>
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%logger"/>
				layout>
			parameter>
			<parameter>
				<parameterName value="@message"/>
				<dbType value="String"/>
				<size value="2000"/>
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%message"/>
				layout>
			parameter>
			<parameter>
				<parameterName value="@exception"/>
				<dbType value="String"/>
				<size value="2000"/>
				<layout type="log4net.Layout.ExceptionLayout"/>
			parameter>
		appender>

	<root>
		
		<appender-ref ref="RollingFileDebug" />
		<appender-ref ref="RollingFileInfo" />
		<appender-ref ref="RollingFileWarn" />
		<appender-ref ref="RollingFileError" />
		<appender-ref ref="RollingFileFatal" />
		<appender-ref ref="logAppender_SqlServer" />		
		<level value="DEBUG" />

	root>

log4net>

你可能感兴趣的:(.NET,.netcore,log4net)