标签:timestamp desc info row rem varchar section base 设置
最近在做一个项目的时候,需要增加一个日志的功能,需要使用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的记录插入。
标签:timestamp desc info row rem varchar section base 设置
原文地址:http://www.cnblogs.com/PatrickLiu/p/6012153.html