• Log4net 自定义字段到数据库(二)


    这种方法比第一种方法麻烦些
    
    Log4Net.config
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <!-- Author:GaoBingBing-->
      <configSections>
        <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
      </configSections>
    
      <log4net>
    
        <!--写入到数据库-->
        <appender name="ADONetAppender_DbServer" type="log4net.Appender.AdoNetAppender">
          <!--错误队列数据达到5个才持久化到数据库-->
          <bufferSize value="1" />
          <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
          <param name="ConnectionString" value="Data Source=ip;initial catalog=test;user id=test;password=****;pooling=true;min pool size=1; max pool size=5;Connection Timeout=100;"/>
          <commandText value="INSERT INTO xiao_log(Date,Thread,Level,Logger,Message,UserId,UserName)VALUES(@log_date, @thread, @log_level, @logger, @message,@userid,@username)"/>
          <parameter>
            <parameterName value="@log_date"/>
            <dbType value="DateTime"/>
    
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
            </layout>
            <!-- <layout type="log4net.Layout.RawTimeStampLayout"/>-->
          </parameter>
          <parameter>
            <parameterName value="@thread"/>
            <dbType value="String"/>
            <size value="255"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%thread"/>
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@log_level"/>
            <dbType value="String"/>
            <size value="50"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%level"/>
            </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="@message"/>
            <dbType value="String"/>
            <size value="4000"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%message"/>
            </layout>
          </parameter>
    
          <!--自定义字段-->
    
          <!--方法二-->
          <parameter>
            <parameterName value="@userid"/>
            <dbType value="String" />
            <size value="255" />
            <layout type="Log4Net.MyLayout">[注意了:这里的Log4Net是命名空间 ,MyLayOut是类名]
              <conversionPattern value="%userid" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@username"/>
            <dbType value="String" />
            <size value="255" />
            <layout type="Log4Net.MyLayout">
              <conversionPattern value="%username" />
            </layout>
          </parameter>
    
        </appender>
        <root>
          <level value="ALL"/>
          <appender-ref ref="ADONetAppender_DbServer"/>
    
        </root>
        <!-- Specify the level for some specific categories -->
        <logger name="iNotes">
          <!-- <appender-ref ref="B" /> -->
          <level value="ALL"/>
          <appender-ref ref="ADONetAppender_DbServer"/>
    
        </logger>
    
    
      </log4net>
    </configuration>
    创建LogMessage.cs,类里面是所有的自定义字段属性
    
    namespace Log4net
    {
        public class LogMessage
        {
            public string _userid;
    
            public string _username;
    
            public LogMessage() { }
    
            public LogMessage(string userId, string userName)
            {
                _userid = userId;
                _username = userName;
            }
    
            public string UserId
            {
                get { return _userid; }
                set { _userid = value; }
            }
    
            public string UserName
            {
                get { return _username; }
                set { _username = value; }
            }
    
        }
    }
    创建  ParemterContent.cs  添加参数转换器类,每个字段一个模式转化类
    
    namespace Log4net
    {
        public class ParemterContent
        {
            internal sealed class UserIdParam : PatternLayoutConverter
            {
                // Methods  
                protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
                {
                    LogMessage content = loggingEvent.MessageObject as LogMessage;
                    if (content != null)
                    {
                        writer.Write(content.UserId);
                    }
                }
            }
    
            internal sealed class UserNameParam : PatternLayoutConverter
            {
                protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
                {
                    LogMessage content = loggingEvent.MessageObject as LogMessage;
                    if (content != null)
                    {
                        writer.Write(content.UserName);
                    }
                }
            } 
    
        }
    } 
    创建 MyLayout.cs  这个是不是很眼熟 <layout type="Log4Net.MyLayout">
    
    
    namespace Log4net
    {
        internal class MyLayout : PatternLayout
        {
    
            public MyLayout()
            {
                this.AddConverter("username", typeof(ParemterContent.UserNameParam));
                this.AddConverter("userid", typeof(ParemterContent.UserIdParam));
            }
        }
    }
        log4net.ILog loger = log4net.LogManager.GetLogger("iNotes");
                    Log4net.LogMessage msg = new LogMessage("45", "dajiahao!");
                    loger.Info(msg);
    如果插入不成功需要检查  Ptoperties下面的AssemblyInfo.cs是不是加入了
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
    
    Global.asax是不是加入了
                LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
    如果缺少则需要加上。
    
    
    如果还不成功 则检查Log4net.config配置文件,肯定xml中自定义属性有问题
  • 相关阅读:
    Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)
    Linux系统级别能够打开的文件句柄的数file-max命令
    记:cloudstack--gluster主存储上的一个文件损坏导致SSVM启动失败
    Linux 之 inotify+rsync 备份文件系统
    为什么KVM计算机点无故重启?
    vim批量在文件每行添加内容以及查询cloudstack模板是否是增量
    记-cloudstack 更改二级存储
    apache 自定义404错误页面
    URL路由
    前端图片优化
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/4686439.html
Copyright © 2020-2023  润新知