c#用log4Net将日志写入到Oracle数据库,并写入到文件中

原文:c#用log4Net将日志写入到Oracle数据库,并写入到文件中

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

1.从官网下载log4net包,安装到项目中;

2.配置文件log4net.config/app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
<appSettings>
<add key="log4net.Internal.Debug" value="true "/>
<add key="DbProvider" value="System.Data.SqlClient"/>
</appSettings> <log4net>
<appender name="AdoNetMysql" type="log4net.Appender.AdoNetAppender,log4net">
<bufferSize value="2"/>
<param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
<param name="ConnectionString" value="Server=DESKTOP-QJVL170;Uid=root;Pwd=zws_321;Database=bjdata_zs;Connect Timeout=120; character set=utf8;Use Affected Rows=true;"/>
<commandText value="INSERT INTO mylog(id,log_datetime,log_thread,log_level,log_logger,log_message) VALUES (null,@log_date, @thread, @log_level, @logger, @message)"/>
<parameter>
<parameterName value="@log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.PatternLayout" value="%d{yyyy-MM-dd HH:mm:ss}"/>
</parameter>
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<layout type="log4net.Layout.PatternLayout" value="%thread">
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<layout type="log4net.Layout.PatternLayout" value="%log_level">
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<layout type="log4net.Layout.PatternLayout" value="%logger">
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<layout type="log4net.Layout.PatternLayout" value="%message">
</layout>
</parameter>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="Info" />
<levelMax value="Fatal" />
</filter>
</appender>
<appender name="ADONetAppender_Oracle" type="log4net.Appender.ADONetAppender">
<!--日志缓存写入条数 设置为0时只要有一条就立刻写到数据库-->
<bufferSize value="0" />
<!--日志数据库连接串-->
<!--<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />-->
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
<!--<connectionString value="Data Source=//59.110.46.189:1521/TestOracle;User ID=TestUser;Password=TestUser;" />-->
<connectionString value="User ID=TestUser;Password=TestUser;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =59.110.46.189)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME =TestOracle )))"/>
<!--<connectionString value="Data Source=TestOracle;User ID=TestUser;Password=TestUser;SERVER=59.110.46.189;" />-->
<!--日志数据库脚本-->
<!--<commandText value="INSERT INTO BJ_LogDetails (LogDate,LogThread,LogLevel,LogLogger,LogActionClick,LogMessage,UserName,UserIP) VALUES ('20180112', '3', 'info', '567657', 'delete', :LOG_MESSAGE,'zs','127.0.0.1')" />-->
<commandText value="INSERT INTO BJ_LOGDETAILS (LogDate,LogThread,LogLevel,LogLogger,LogActionClick,LogMessage,OPERAND) VALUES (:LogDate,:LogThread,:LogLevel,:LogLogger,:ActionsClick,:Message,:Operand)" />
<!--日志时间LogDate -->
<parameter>
<parameterName value="@log_date" />
<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="%t" />
</layout>
</parameter>
<!--日志类型LogLevel -->
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<!--日志名称-->
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="500" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@ActionsClick" />
<dbType value="String" />
<size value="4000" />
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog" >
<conversionPattern value = "%property{ActionsClick}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<size value="2000" />
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
<conversionPattern value="%property{Message}" />
</layout>
</parameter>
<!--操作对象-->
<parameter>
<parameterName value="@Operand" />
<dbType value="String" />
<size value="300" />
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
<conversionPattern value="%property{Operand}" />
</layout>
</parameter>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<!--定义文件存放位置-->
<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 ==== : %newline%message%newline%newline" />
</layout>
</appender>
<!--配置到数据库-->
<appender name="ADONetApp" type="log4net.Appender.ADONetAppender,log4net">
<!--BufferSize为缓冲区大小-->
<bufferSize value="10" /> <!--<param name="BufferSize" value="2" />-->
<!--引用-->
<param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
<!--连接字符串-->
<param name="ConnectionString" value="Server=DESKTOP-QJVL170;Uid=root;Pwd=zws_321;Database=bjdata_zs;Allow Zero Datetime=True;character set=utf8;"/>
<!--插入语句-->
<commandText value="INSERT INTO sys_error_log(Message) VALUES ('sdfd')" />
<!--记录时间-->
<!--<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>-->
<!--线程号--> <!--消息-->
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<size value="2000" />
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
<conversionPattern value="%property{Message}" />
</layout>
</parameter>
</appender> <root>
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="ADONetAppender_Oracle"/>
<!--记录到什么介质中-->
</root>
<!--使用反射方法同时写两文件和数据库-->
<logger name="writLogger">
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="ADONetAppender_Oracle"/>
</logger> </log4net>
<connectionStrings>
<add name="OraConnString1" connectionString="User ID=TestUser;Password=TestUser;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =59.110.46.189)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME =TestOracle )))"/>
<add name="OracleTarget" connectionString="User ID=TESTUSER;Password=TestUser;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =59.110.46.189)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME =BJWARE )))"/>
</connectionStrings>
</configuration>

3.c#代码的调用:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
log4net.Config.XmlConfigurator.Configure();
} private void button1_Click(object sender, EventArgs e)
{
ILog log1 = LogManager.GetLogger("writLogger"); ILog log11 = LogManager.GetLogger("writLogger");
log11.Info(new LogInfo("1","wgh","s","登陆"));
log1.Info(new LogMessage(1, (int)TGLog.ActionType.Other, "测试数据"));
} private void Form1_Load(object sender, EventArgs e)
{ }
}

4.数据库表结构如下:

CREATE TABLE "TESTUSER"."BJ_LOGDETAILS"
( "LOGID" NUMBER,
"LOGDATE" DATE,
"LOGTHREAD" NVARCHAR2(100),
"LOGLEVEL" NVARCHAR2(200),
"LOGLOGGER" NVARCHAR2(500),
"LOGMESSAGE" NVARCHAR2(2000),
"LOGACTIONCLICK" NVARCHAR2(400),
"OPERAND" NVARCHAR2(400)
)

c#用log4Net将日志写入到Oracle数据库,并写入到文件中c#用log4Net将日志写入到Oracle数据库,并写入到文件中PS:1.配置文件中,不需要的参数必须注释或者删除,否则写入不到数据库中;

2.确保链接字符串,SQL语句的正确率;

3.确保oracle数据库版本正确,确保log4net包版本正确;

4.调试阶段,可以跟踪ILog中的参数是否全部为true;

如有错误,希望指出;共同进步!

上一篇:MSSQL备份脚本


下一篇:OpenGL 阴影之Shadow Mapping和Shadow Volumes