前言
Log4net 库是 Apache log4j 框架在 Microsoft .NET 平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台、文件、数据库等)的工具。
log4net 是 Apache 软件基金会 Apache Logging Services 工程的一部分。Apache 日志服务工程致力于为程序调试和审计提供跨语言的日志服务。
官方配置文件样例:https://logging.apache.org/log4net/release/config-examples.html
日志组件 Log4net
NuGet 引入程序包
log4net.xml 配置文件
ConfigFile/log4net.xml
提示:配置文件需要设置为 始终复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log4/" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppend+MinimalLock"/>
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd'.log'" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="3MB" />
<layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>
<root> <priority value="ALL"/>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" /> </root> </log4net>
|
使配置文件生效
NuGet 中需要引用Microsoft.Extensions.Logging.Log4Net.AspNetCore
1 2
| using Microsoft.Extensions.Logging.Log4Net.AspNetCore; builder.Logging.AddLog4Net("ConfigFile/log4net.xml");
|
开始使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class HomeController : Controller { private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger) { _logger = logger; _logger.LogInformation($"{this.GetType().Name} 被构造了!"); }
public IActionResult Index() {
try { List<string> list = null; list.Add("a"); } catch (Exception ex) { _logger.LogError(ex, "Index 执行报错了!"); }
_logger.LogInformation($"Index 被执行了!"); return View(); } }
|
日志组件 Log4net 写入 SqlServer
NuGet 引入程序包
创建日志表
1 2 3 4 5 6 7 8 9
| CREATE TABLE [dbo].[Log] ( [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 )
|
编辑配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" /> <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>
...
<appender-ref ref="AdoNetAppender" />
|