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> <!-- Oracle Appender --> <appender name="ADONetAppender_Oracle" type="log4net.Appender.ADONetAppender"> <!--bufferSize 为缓冲区大小,只有日志记录超设定值才会一块写入到数据库 --> <bufferSize value="0" /> <!--访问数据库驱动的引用--> <connectionType value="Oracle.DataAccess.Client.OracleConnection,Oracle.DataAccess,Version=2.112.1.0,Culture=neutral,PublicKeyToken=89b483f429c47342" /> <!--数据库连接字符串--> <connectionString value="Data Source=192.168.7.9/ORCL;User ID=CrossBorderEBusiness_PHP;Password=CrossBorderEBusiness_PHP;" /> <!--数据库执行的sql语句--> <commandText value="INSERT INTO LOG_LOG4NET (LOG_ID,LOG_DATE,LOG_LEVEL,LOG_IDENTITY,LOG_MESSAGE,LOG_EXCEPTION,LOG_LOGGER,LOG_SOURCE) VALUES (LOG_LOG4NET_SEQ.nextval,:log_date,:log_level,:log_identity,:log_message,:log_exception,:logger,:source)" /> <!-- 日志记录时间,RawTimeStampLayout为默认的时间输出格式 --> <parameter> <parameterName value=":log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout"> </layout> </parameter> <!--日志等级--> <parameter> <parameterName value=":log_level" /> <dbType value="String" /> <size value="10" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value=":log_identity" /> <dbType value="String" /> <size value="100" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%identity" /> </layout> </parameter> <!-- 自定义的消息 --> <parameter> <parameterName value=":log_message" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%m" /> </layout> </parameter> <!-- 异常信息 --> <parameter> <parameterName value=":log_exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%exception" /> </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=":source" /> <dbType value="String" /> <size value="1000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%file:%line" /> </layout> </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_Oracle" /> </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.Oracle { 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("OracleTest" + i); } } } }
Oracle脚本:
-- Create table create table LOG_LOG4NET ( LOG_ID NUMBER not null,LOG_DATE DATE,LOG_LEVEL VARCHAR2(255),LOG_IDENTITY VARCHAR2(255),LOG_MESSAGE NVARCHAR2(2000),LOG_EXCEPTION NVARCHAR2(2000),LOG_LOGGER NVARCHAR2(255),LOG_SOURCE NVARCHAR2(1000) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Create sequence create sequence LOG_LOG4NET_SEQ minvalue 1 maxvalue 999999999999999999999999999 start with 221 increment by 1 cache 20;
测试结果,如下图:
测试时注意修改节点 :connectionString 修改为 自己测试环境的连接字符串
注意点:
<!--bufferSize 为缓冲区大小,只有日志记录超设定值才会一块写入到数据库 -->
<!-- 注意bufferSize 参数在测试环境建议设置为 0 ,在生产环境建议设置为100 -->
<bufferSize value="0" />
如果测试环境设置成 100 ,则缓冲区 超过100条 消息才会 写入sqlServer库,这个很容易忘记,造成不必要的问题。测试环境 请设置成 0