欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-18 14:23:14
概述 Log4net保存自定义字段到数据库,此处以oracle数据库为例 实现 1、下载log4net,添加log4net引用。 2、创建数据库 CREATE TABLE TASKDB.WORKFLOWLOGS( LOG_ID NUMBER NOT NULL, LOG_USERID VARCHAR2 ......

概述

       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为例)

 

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