最近在做一個項目的時候,需要增加一個日志的功能,需要使用Log4Net記錄日志,把數據插入到Oracle數據庫,經過好久的研究終於成功了。把方法記錄下來,以備以後查詢。
直接寫實現方法,分兩步完成:
1、使用NuGet Manager管理工具,增加對Oracle.ManagedDataAccess.dll的引用。
2、配置具體的文件。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true "/>
</appSettings>
<log4net>
<!-- 產生數據的腳本
Create table T_LogHistory
(
id NUMBER not null,
log_date DATE not null,
log_thread VARCHAR2(50) not null,
log_level VARCHAR2(50) not null,
log_logger VARCHAR2(150) not null,
log_operator VARCHAR2(50) not null,
log_message VARCHAR2(500) not null,
log_iP VARCHAR2(50) not null,
log_machineName VARCHAR2(50) not null,
log_browser VARCHAR2(50) not null,
log_location VARCHAR2(500) not null,
log_exception VARCHAR2(1500) not null,
constraint pk_LogHistory primary key(id)
);
Create Sequence seq_LogHistory_ID
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1
nocache;
-->
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender,log4net">
<!--BufferSize為緩沖區大小,只有日志記錄超設定值才會一塊寫入到數據庫-->
<bufferSize value="1" />
<!--或寫為<param name="BufferSize" value="10" />-->
<!--引用-->
<!--<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>-->
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
<!--連接數據庫字符串-->
<connectionString value="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=80))(CONNECT_DATA=(SERVICE_NAME=databaseName)));User Id=*******;Password=*******;" />
<!--插入到表Log-->
<commandText value="insert into t_loghistory(id, log_date, log_thread, log_level, log_logger, log_operator, log_message, log_ip, log_machinename, log_browser, log_location, log_exception) values(SEQ_LOGHISTORY_ID.nextval, :logDate, :thread, :log_level, :logger, :operator, :message, :clientIP, :machineName, :browser, :location, :exception)" />
<!--日志記錄時間,RawTimeStampLayout為默認的時間輸出格式-->
<parameter>
<parameterName value=":logDate" />
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<!--線程號-->
<parameter>
<parameterName value=":thread" />
<dbType value="String" />
<!--長度不可以省略,否則不會輸出-->
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<!--日志等級-->
<parameter>
<parameterName value=":log_level" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<!--日志記錄類名稱-->
<parameter>
<parameterName value=":logger" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<!--操作者。這個是自定義的輸出字段,使用重新實現的布局器ReflectionLayout-->
<parameter>
<parameterName value=":operator" />
<!--設置為Int32時只有bufferSize的 value<="1"才正確輸出,沒有找出原因。-->
<dbType value="String" />
<size value="200"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%username" />
</layout>
</parameter>
<!--IP地址-->
<parameter>
<parameterName value=":clientIP" />
<dbType value="String" />
<size value="20" />
<layout type="Enterprise.Framework.Logger.Extensions.ClientIPLayout,Enterprise.Framework.Logger">
<conversionPattern value="%ClientIP"/>
</layout>
</parameter>
<!--機器名-->
<parameter>
<parameterName value=":machineName" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}" />
</layout>
</parameter>
<!--浏覽器-->
<parameter>
<parameterName value=":browser" />
<dbType value="String" />
<size value="50" />
<layout type="Enterprise.Framework.Logger.Extensions.BrowserLayout">
<conversionPattern value="%Browser" />
</layout>
</parameter>
<!--日志消息-->
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<size value="3000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<!--記錄日志的位置-->
<parameter>
<parameterName value=":location" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%location" />
</layout>
</parameter>
<!--異常信息。ExceptionLayout 為異常輸出的默認格式-->
<parameter>
<parameterName value=":exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<!--高 OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL 低-->
<level value="ALL"/>
<appender-ref ref="ADONetAppender" />
</root>
<!--<logger name="StellaLogger">
<level value="ALL"/>
<appender-ref ref="AdoNetAppender" />
</logger>-->
</log4net>
</configuration>
完成上面兩步就可以實現Log4Net針對數據庫Oracle的記錄插入。