Recently in a project, need to add a log function, need to use log4net log, data inserted into the Oracle database, after a long time the study finally succeeded. Record the method for future enquiries.
Direct write implementation method, in two steps to complete:
1. Use the NuGet Manager management tool to add a reference to the Oracle.ManagedDataAccess.dll.
2, configure the specific files.
<?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>
<!--script to generate data
Create Table T_loghistory
(
ID number is not NULL,
Log_date date NOT NULL,
Log_thread VARCHAR2 () NOT NULL,
Log_level VARCHAR2 () NOT NULL,
Log_logger VARCHAR2 () NOT NULL,
Log_operator VARCHAR2 () NOT NULL,
Log_message VARCHAR2 (+) NOT NULL,
Log_ip VARCHAR2 () NOT NULL,
Log_machinename VARCHAR2 () NOT NULL,
Log_browser VARCHAR2 () NOT NULL,
Log_location VARCHAR2 (+) NOT NULL,
Log_exception VARCHAR2 () 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 is the buffer size, only the logging Hyper-SetPoint is written to the database--
<buffersize value= "1"/>
<!--or written as <param name= "buffersize" value= "ten"/>-->
<!--reference-->
<!--<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 "/>
<!--connection database string-->
<connectionstring value= "Data Source= (description= (address= (protocol=tcp) (host=127.0.0.1) (port=80)) (Connect_data= (Service_name=databasename) )); User id=*******; password=*******; "/>
<!--inserting into table 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) "/& Gt
<!--logging time, rawtimestamplayout as the default time output format-
<parameter>
<parametername value= ": Logdate"/>
<dbtype value= "DateTime"/>
<layout type= "log4net. Layout.rawtimestamplayout "/>
</parameter>
<!--thread number-->
<parameter>
<parametername value= ": Thread"/>
<dbtype value= "String"/>
<!--length can not be omitted, otherwise it will not output-->
<size value= "" "/>
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%thread"/>
</layout>
</parameter>
<!--log level-->
<parameter>
<parametername value= ": Log_level"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%level"/>
</layout>
</parameter>
<!--log record class name-->
<parameter>
<parametername value= ": Logger"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%logger"/>
</layout>
</parameter>
<!--operator. This is the custom output field, using the re-implemented layout reflectionlayout-->
<parameter>
<parametername value= ": operator"/>
<!--set to Int32 only buffersize value<= "1" output correctly, no reason is found. -->
<dbtype value= "String"/>
<size value= "/>",
<layout type= " Log4net. Layout.patternlayout,
<conversionpattern value= "%username"/>
</layout>
</parameter>
<!--IP address-->
<parameter>
<parametername value= ": ClientIP"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= " Enterprise.framework.logger.extensions.clientiplayout,enterprise.framework.logger,
<conversionpattern value= "%clientip"/>
</layout>
</parameter>
<!--machine name-->
<parameter>
<parametername value= ": MachineName"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%property{log4net:hostname}"/>
</layout>
</parameter>
<!--browser-->
<parameter>
<parametername value= ": Browser"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= " Enterprise.Framework.Logger.Extensions.BrowserLayout,
<conversionpattern value= "%browser"/>
</layout>
</parameter>
<!--log messages-->
<parameter>
<parametername value= ": Message"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%message"/>
</layout>
</parameter>
<!--log location-->
<parameter>
<parametername value= ": Location"/>
<dbtype value= "String"/>
<size value= "/>",
<layout type= "log4net. Layout.patternlayout,
<conversionpattern value= "%location"/>
</layout>
</parameter>
<!--exception information. Exceptionlayout is the default format for exception output--
<parameter>
<parametername value= ": Exception"/>
<dbtype value= "String"/>
<size value= "4000"/>
<layout type= "log4net. Layout.exceptionlayout "/>
</parameter>
</appender>
<root>
<!--high OFF, FATAL, ERROR, WARN, INFO, DEBUG, all low--and
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>
</root>
<!--<logger name= "Stellalogger" >
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>
</logger>-->
</log4net>
</configuration>
Complete the above two steps to achieve log4net for database Oracle record insertion.
How to configure Log4net to use Oracle database logging