[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"?>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd.TXT" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="3MB" />
<staticLogFileName value="true" />
<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中配置数据库
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<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>
<priority value="ALL"/>
<level value="INFO"/>
<appender-ref ref="rollingAppender" />
<appender-ref ref="AdoNetAppender_SqlServer" />
</root>
3、读取配置文件,使用log4net
在Program中配置
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()
中找到
- 37.2w
-
掘金·日新计划
Apache Log4j
- 4410
-
zxg_神说要有光
JavaScript
- 6.8w
-
zxg_神说要有光
JavaScript
Node.js
- 2585
-
纯情的小太阳