一、环境说明:
开发工具:vs2010 ,数据库:oracle 11g ,版本:log4net的目前最新版本1.2.13.0 ; Oracle.ManagedDataaccess.dll Version 4.121.1.0
二、官网dll准备
log4net http://mirrors.hust.edu.cn/apache//logging/log4net/binaries/log4net-1.2.13-bin-newkey.zip
Oracle.ManagedDataAccess.dll 官网http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html
三、实现步骤(以个人建立的项目为例子解说)
1、建立项目log4netTest5,把dll放在Lib文件内, 打开项目 引用 把dll 引入项目
2、建立log4net.config 配置文件(周公等网上的达人都详解的很清楚了,我就讲解oracle哪一部分)
注意: <appender name="AdoNetAppender_ORCL" type="log4net.Appender.OracleAppender">数据驱动
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection,Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />数据库连接串
<connectionString value="Data Source=//localhost:1521/orcl;User ID=user;PassWord=user;" />还有
<commandText value="insert into SECURITY_LOG4NET 按照自己的实际情况自己修改
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="AdoNetAppender_ORCL" type="log4net.Appender.OracleAppender"> <bufferSize value="1"/> <connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection,Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" /> <connectionString value="Data Source=//localhost:1521/orcl;User ID=user;Password=user;" /> <commandText value="insert into SECURITY_LOG4NET (LOG_DATE,LOG_LEVEL,LOG_MESSAGE,LOG_EXCEPTION,LOG_LOGGER,LOG_SOURCE,USERID,LOGTYPE) VALUES (:log_date, :log_level,:log_message, :log_exception, :logger, :source,:USERID,:LOGTYPE)"/> <parameter> <parameterName value=":log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout"> <conversionPattern value="%d{yyyy/MM/dd HH:mm:ss}" /> </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_message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%m" /> </layout> </parameter> <parameter> <parameterName value=":log_exception" /> <dbType value="String" /> <size value="4000" /> <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> <parameter> <parameterName value=":USERID" /> <dbType value="String" /> <size value="100" /> <layout type="Log4NetToDatabase.CustomLayout"> <conversionPattern value="%USERID" /> </layout> </parameter> <parameter> <parameterName value=":LOGTYPE" /> <dbType value="String" /> <size value="100" /> <layout type="Log4NetToDatabase.CustomLayout"> <conversionPattern value="%LOGTYPE" /> </layout> </parameter> </appender> <root> <level value ="ALL"/> <appender-ref ref="AdoNetAppender_ORCL"/> </root> </log4net></configuration>
3、自己实现OracleAppender.cs 和OracleAppenderParameter.cs
(我直接取自http://www.VEVb.com/hnsongbiao/p/4216147.html#commentform)
1 public class OracleAppender : BufferingAppenderSkeleton 2 { 3 // Fields 4 PRivate static readonly Type declaringType = typeof(AdoNetAppender); 5 private string m_commandText; 6 private CommandType m_commandType = CommandType.Text; 7 private string m_connectionString; 8 private string m_connectionType; 9 private OracleCommand m_dbCommand; 10 private OracleConnection m_dbConnection; 11 protected ArrayList m_parameters = new ArrayList(); 12 private bool m_reconnectOnError = false; 13 private SecurityContext m_securityContext; 14 protected bool m_usePreparedCommand; 15 private bool m_useTransactions = true; 16 17 // Methods 18 public override void ActivateOptions() 19 { 20 base.ActivateOptions(); 21 this.m_usePreparedCommand = (this.m_commandText != null) && (this.m_commandText.Length > 0); 22 if (this.m_securityContext == null) 23 { 24 this.m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this); 25 } 26 this.InitializeDatabaseConnection(); 27 this.InitializeDatabaseCommand(); 28 } 29 30 public void AddParameter(OracleAppenderParameter parameter) 31 { 32 this.m_parameters.Add(parameter); 33 } 34 35 protected virtual string GetLogStatement(LoggingEvent logEvent) 36 { 37 if (this.Layout == null) 38 { 39 this.ErrorHandler.Error("ADOAppender: No Layout specified."); 40 return ""; 41 } 42 StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); 43 this.Layout.Format(writer, logEvent); 44 return writer.ToString(); 45 } 46 47 private void InitializeDatabaseCommand() 48 { 49 if ((this.m_dbConnection != null) && this.m_usePreparedCommand) 50 { 51 try 52 { 53 this.m_dbCommand = this.m_dbConnection.CreateCommand(); 54 this.m_dbCommand.CommandText = this.m_commandText; 55 this.m_dbCommand.CommandType = this.m_commandType; 56 } 57 catch (Exception exception) 58 { 59 this.ErrorHandler.Error("Could not create database command [" + this.m_commandText + "]", exception); 60 if (this.m_dbCommand != null) 61 { 62 try 63 { 64 this.m_dbCommand.Dispose(); 65 } 66 catch 67 { 68 } 69 this.m_dbCommand = null; 70
新闻热点
疑难解答