相关文章推荐
​ ​ ​ ​[Id] ​[int] ​IDENTITY ​( 1 , ​ 1 ) ​NOT ​NULL, ​ ​ ​ ​ [Date] [datetime] ​NOT ​NULL, ​ ​ ​ ​ [Thread] [varchar] ​( 255 ) ​NOT ​NULL, ​ ​ ​ ​ [Level] [varchar] ​( 50 ) ​NOT ​NULL, ​ ​ ​ ​ [Logger] [varchar] ​( 255 ) ​NOT ​NULL, ​ ​ ​ ​ [Message] [varchar] ​( 4000 ) ​NOT ​NULL, ​ ​ ​ ​ [Exception] [varchar] ​( 2000 ) ​NULL

2、添加一个.config(对log4net的配置)

<?xml version="1.0" encoding="utf-8"?>
	<!--防止多线程时不能写Log,官方说线程非安全-->
	<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
	<!--可以为:Once|Size|Date|Composite-->
	<!--Composite为Size和Date的组合-->
	<rollingStyle value="Composite" />
	<!--当备份文件时,为文件名加的后缀-->
	<datePattern value="yyyyMMdd.TXT" />
	<!--日志最大个数,都是最新的-->
	<!--rollingStyle节点为Size时,只能有value个日志-->
	<!--rollingStyle节点为Composite时,每天有value个日志-->
	<maxSizeRollBackups value="20" />
	<!--可用的单位:KB|MB|GB-->
	<maximumFileSize value="3MB" />
	<!--置为true,当前最新日志文件名永远为file节中的名字-->
	<staticLogFileName value="true" />
	<!--输出级别在INFO和ERROR之间的日志-->
	<filter type="log4net.Filter.LevelRangeFilter">
		<param name="LevelMin" value="ALL" />
		<param name="LevelMax" value="FATAL" />
	</filter>
	<layout type="log4net.Layout.PatternLayout">
		<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
	</layout>
</appender>

在log4net中配置数据库

<!--SqlServer形式-->
<!--log4net日志配置:http://logging.apache.org/log4net/release/config-examples.html -->
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
	<!--日志缓存写入条数 设置为0时只要有一条就立刻写到数据库-->
	<bufferSize value="0" />
	<connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient, Version=4.6.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
	<connectionString value="数据库链接字符串" />
	<commandText value="INSERT INTO Log ([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="255" />
		<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="255" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%logger" />
		</layout>
	</parameter>
	<parameter>
		<parameterName value="@message" />
		<dbType value="String" />
		<size value="4000" />
		<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>
	<!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
	<!--OFF:0-->
	<!--FATAL:FATAL-->
	<!--ERROR: ERROR,FATAL-->
	<!--WARN: WARN,ERROR,FATAL-->
	<!--INFO: INFO,WARN,ERROR,FATAL-->
	<!--DEBUG: INFO,WARN,ERROR,FATAL-->
	<!--ALL: DEBUG,INFO,WARN,ERROR,FATAL--> 
	<priority value="ALL"/>
	<level value="INFO"/>
	<appender-ref ref="rollingAppender" />
	<appender-ref ref="AdoNetAppender_SqlServer" />
</root>

3、读取配置文件,使用log4net 在Program中配置

//using Microsoft.AspNetCore.Authentication.Cookies;
builder.Logging.AddLog4Net("CfgFile/log4net.Config(配置文件的路径)");

4、添加一个Home2Controller控制器中使用构造函数(ILogger<>是日志的专用数组)

public class Home2Controller : Controller
    private readonly ILogger<Home2Controller> _Logger;
    private readonly ILoggerFactory _LoggerFactory;
    public Home2Controller(ILogger<Home2Controller> logger, ILoggerFactory loggerFactory)
        _Logger = logger;
        _Logger.LogInformation($"{GetType().Name}被构造了");
        _LoggerFactory = loggerFactory;
        ILogger<Home2Controller> logger1 = _LoggerFactory.CreateLogger<Home2Controller>();
        logger1.LogInformation($"{GetType().Name}被构造了");
    public IActionResult Index()
        ILogger<Home2Controller> logger1 = _LoggerFactory.CreateLogger<Home2Controller>();
        _Logger.LogInformation($"index被执行");
        return View();

中找到

  •  
    推荐文章