使用Log4Net - 文件日志 和 SqlServer日志
Log4Net.config 配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!-- Define some output appenders -->
<!--定义输出到文件中-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="log\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMM\\yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value="100"/>
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> : %newline%message%newline%newline" />-->
<conversionPattern value="==== %date [%thread] %-5level ==== : %newline%message%newline%newline" />
</layout>
</appender>
<!--Sql server Appender -->
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.ADONetAppender">
<!--bufferSize 为缓冲区大小,只有日志记录超设定值才会一块写入到数据库 -->
<!-- 注意bufferSize 参数在测试环境建议设置为 0 ,在生产环境建议设置为100 -->
<bufferSize value="0" />
<!--访问数据库驱动的引用-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!--数据库连接字符串-->
<connectionString value="server=.;database=Log4Net;uid=sa;password=123.com;"/>
<!--数据库执行的Sql语句-->
<commandText value="INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<!-- 日志记录时间,RawTimeStampLayout为默认的时间输出格式 -->
<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>
<!--日志等级 级别从低到高分别是:debug(调试),info(信息),warn(警告),error(错误),fatal(致命错误)-->
<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>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="AdoNetAppender_SqlServer" />
</root>
</log4net>
</configuration>
Program.cs代码,如下:
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Content/Config/Log4Net.config", Watch = true)]
namespace Com.No1.Log4Net.SqlServer
{
class Program
{
protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
for (int i = 0; i < 101; i++)
{
logger.Debug("Test" + i);
}
}
}
}
数据库创建表脚本:
CREATE TABLE [dbo].[Log4Net](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Date] [datetime2](7) NULL,
[Thread] [varchar](255) NULL,
[Level] [varchar](255) NULL,
[Logger] [varchar](255) NULL,
[Message] [varchar](255) NULL,
[Exception] [varchar](255) NULL,
CONSTRAINT [PK_Log4Net] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
结果,如下图:
测试时注意修改节点 : connectionString 修改为 自己测试环境的连接字符串
注意点:
<!--bufferSize 为缓冲区大小,只有日志记录超设定值才会一块写入到数据库 -->
<!-- 注意bufferSize 参数在测试环境建议设置为 0 ,在生产环境建议设置为100 -->
<bufferSize value="0" />
如果测试环境设置成 100 ,则缓冲区 超过100条 消息才会 写入SqlServer库,这个很容易忘记,造成不必要的问题。测试环境 请设置成 0