Log4net保存自定义字段到数据库(以oracle为例)

  • A+
所属分类:.NET技术
摘要

       Log4net保存自定义字段到数据库,此处以oracle数据库为例       1、下载log4net,添加log4net引用。 


概述

       Log4net保存自定义字段到数据库,此处以oracle数据库为例

实现

       1、下载log4net,添加log4net引用。 

       2、创建数据库

CREATE TABLE TASKDB.WORKFLOWLOGS
(
  LOG_ID NUMBER NOT NULL,
  LOG_USERID VARCHAR2(100 BYTE),
  LOG_OPERATERTYPE VARCHAR2(100 BYTE),
  LOG_MESSAGE VARCHAR2(4000 BYTE),
  LOG_DATE DATE,
  LOG_LEVEL VARCHAR2(255 BYTE),
  LOG_IDENTITY VARCHAR2(255 BYTE),
  LOG_EXCEPTION VARCHAR2(4000 BYTE),
  LOG_LOGGER VARCHAR2(255 BYTE),
  LOG_SOURCE VARCHAR2(1000 BYTE)
)

  3、添加自定义字段的实体类  

namespace Log4NetToDatabase {     /// <summary>     /// 包含了所有的自定字段属性     /// </summary>     public class LogContent : IRequiresSessionState     {          public LogContent(string _UserId, string _OperaterType, string _Message)         {             UserId = _UserId;             OperaterType = _OperaterType;             Message = _Message;         }           public string UserId { get; set; }         public string OperaterType { get; set; }         public string Message { get; set; }                }        }

 

  4、添加参数转换器类,每个字段一个转化类

namespace Log4NetToDatabase {     public class Log4NetConvert     {         internal sealed class UserIdPatternConverter : PatternLayoutConverter         {             override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)             {                 LogContent content = loggingEvent.MessageObject as LogContent;                 if (content != null)                 {                     writer.Write(content.UserId);                 }             }         }          internal sealed class OperaterTypePatternConverter : PatternLayoutConverter         {             override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)             {                 LogContent content = loggingEvent.MessageObject as LogContent;                 if (content != null)                 {                     writer.Write(content.OperaterType);                 }             }         }           internal sealed class MessagePatternConverter : PatternLayoutConverter         {             override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)             {                 LogContent content = loggingEvent.MessageObject as LogContent;                 if (content != null)                 {                     writer.Write(content.Message);                 }             }         }                }  }

  5、添加自定义layout类

namespace Log4NetToDatabase {     public class CustomLayout : PatternLayout     {         public CustomLayout()         {             //this.AddConverter("property", typeof(Log4NetConvert));             this.AddConverter("UserId", typeof(Log4NetConvert.UserIdPatternConverter));             this.AddConverter("OperaterType", typeof(Log4NetConvert.OperaterTypePatternConverter));             this.AddConverter("Message", typeof(Log4NetConvert.MessagePatternConverter));         }     } }

 

  6 、添加配置信息

<?xml version="1.0" encoding="utf-8" ?>  <configuration>     <configSections>         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>     </configSections>     <log4net>         <!--日志记录器-->         <logger name="OperationLog" additivity="false">             <level value="ALL"/>             <!--设置使用的数据库记录器-->             <appender-ref ref="AdoNetAppender_Operation"/>         </logger>          <!--数据库记录器-->         <appender name="AdoNetAppender_Operation" type="log4net.Appender.AdoNetAppender">             <!--sqlserver日志数据库连接串-->             <!--<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>       <connectionString value="Data Source=.sqlexpress;initial catalog=kuiyu.net;User ID=sa;Password=1"  providerName="System.Data.SqlClient"/>-->             <!--oracle日志数据库连接串-->             <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>              <connectionString value="Data Source=//localhost:1521/orcl;User ID=xxxxdb;Password=xxxxdb;"/>              <!--日志数据库脚本-->             <commandText value="INSERT INTO WORKFLOWLOGS (LOG_ID,LOG_USERID,LOG_OPERATERTYPE,  LOG_MESSAGE, LOG_DATE, LOG_LEVEL, LOG_IDENTITY, LOG_EXCEPTION, LOG_LOGGER, LOG_SOURCE) VALUES (WORKFLOWLOGS_SEQ.nextval,                      :UserId,:OperaterType, :Message,:log_date, :log_level, :log_identity,:log_exception, :logger, :source )" />               <bufferSize value="1"/>             <!--自定义成员 -->              <parameter>                 <parameterName value=":UserId" />                 <dbType value="String" />                 <size value="1000" />                 <layout type="Log4NetToDatabase.CustomLayout">                     <conversionPattern value="%UserId" />                 </layout>              </parameter>              <parameter>                 <parameterName value=":OperaterType" />                 <dbType value="String" />                 <size value="1000" />                 <layout type="Log4NetToDatabase.CustomLayout">                     <conversionPattern value="%OperaterType" />                 </layout>              </parameter>               <parameter>                 <parameterName value=":Message" />                 <dbType value="String" />                 <size value="4000" />                 <layout type="Log4NetToDatabase.CustomLayout">                     <conversionPattern value="%Message" />                 </layout>              </parameter>              <parameter>                 <parameterName value=":log_date" />                 <dbType value="DateTime" />                 <layout type="log4net.Layout.RawTimeStampLayout">                 </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_identity" />                 <dbType value="String" />                 <size value="100" />                 <layout type="log4net.Layout.PatternLayout">                     <conversionPattern value="%identity" />                 </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>                      </appender>                                </log4net> </configuration>

 

  7、调用写入日志

ILog log = log4net.LogManager.GetLogger("OperationLog");  log.Info(new LogContent(dt_user.Rows[0]["userid"].ToString(), "登录", "登陆系统"));

Log4net保存自定义字段到数据库(以oracle为例)

 

 

 

总结

       添加自定义字段的实体类

  添加参数转换器类

  添加自定义layout类

    添加配置信息

       调用写入日志Log4net保存自定义字段到数据库(以oracle为例)

 

若对您有用,请赞助个棒棒糖~