首页 新闻 会员 周边 捐助

log4net自定义字段无法插入数据库

0
悬赏园豆:50 [待解决问题]

log4net.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>  
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=8.0.11.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
      <connectionString value="server=120.78.75.199;port=3306;database=logtest;uid=root;password=4511027;SslMode = None" />
      <commandText  value="INSERT INTO log(Log_DateTime,Log_Thread,Log_Level,Log_Logger,Log_Message) VALUES(@log_datetime, @log_thread , @log_level, @log_logger, @log_message)" />
      <logger name="iNotes">
         <appender-ref ref="ADONetAppender" /> 
        <level value="ALL"/>
      </logger>     
      <param name="Parameter">
        <param name="ParameterName" value="@log_datetime" />
        <param name="DbType" value="DateTime" />
        <param name="Layout" type="test.MyLaout">
          <param name="ConversionPattern" value="%log_datetime" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@log_thread" />
        <param name="DbType" value="String" />
        <param name="Size" value="255" />
        <param name="Layout" type="test.MyLaout">
          <param name="ConversionPattern" value="%log_thread" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@log_level" />
        <param name="DbType" value="String" />
        <param name="Size" value="50" />
        <param name="Layout" type="test.MyLaout">
          <param name="ConversionPattern" value="%log_level" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@log_logger" />
        <param name="DbType" value="String" />
        <param name="Size" value="255" />
        <param name="Layout" type="test.MyLaout">
          <param name="ConversionPattern" value="%log_logger" />
        </param>
      </param>
      <param name="Parameter">
        <param name="ParameterName" value="@log_message" />
        <param name="DbType" value="String" />
        <param name="Size" value="255" />
        <param name="Layout" type="test.MyLaout">
          <param name="ConversionPattern" value="%log_message" />
        </param>
      </param>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ADONetAppender" />
      <appender-ref ref="LogFileAppender"/>  
    </root>
  </log4net>


  
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
  </system.web>

</configuration>

Log.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace test
{
    public class Log 
    {
        public DateTime Log_DateTime { get; set; }

        public string Log_Thread { get; set; }

        public string Log_Level { get; set; }
        public string Log_Logger { get; set; }

        public string Log_Message { get; set; }

        public Log( DateTime log_DateTime,string log_Thread, string log_Level,string log_Logger,string log_Message)
        {
            Log_DateTime = log_DateTime;
            Log_Thread = log_Thread;
            Log_Level = log_Level;
            Log_Logger = log_Logger;
            Log_Message = log_Message;
        }      
    }
}

MyLayout.cs

namespace test
{
    public class MyLayout : PatternLayout
    {
        public MyLayout()
        {
            this.AddConverter("log_datetime", typeof(ParamterContent.Log_DateTimeParam));
            this.AddConverter("log_thread", typeof(ParamterContent.Log_ThreadParam));
            this.AddConverter("log_level", typeof(ParamterContent.Log_LevelParam));
            this.AddConverter("log_logger", typeof(ParamterContent.Log_LoggerParam));
            this.AddConverter("log_message", typeof(ParamterContent.Log_MessageParam));
        }
    }
}

ParamterContent.cs

        internal sealed class Log_DateTimeParam : PatternLayoutConverter
        {
            // Methods  
            protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
            {
                Log content = loggingEvent.MessageObject as Log;
                if (content != null)
                {
                    writer.Write(content.Log_DateTime);
                }
            }
        }

        internal sealed class Log_ThreadParam : PatternLayoutConverter
        {
            protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
            {
                Log content = loggingEvent.MessageObject as Log;
                if (content != null)
                {
                    writer.Write(content.Log_Thread);
                }
            }
        }
        internal sealed class Log_LevelParam : PatternLayoutConverter
        {
            protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
            {
                Log content = loggingEvent.MessageObject as Log;
                if (content != null)
                {
                    writer.Write(content.Log_Level);
                }
            }
        }
        internal sealed class Log_LoggerParam : PatternLayoutConverter
        {
            protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
            {
                Log content = loggingEvent.MessageObject as Log;
                if (content != null)
                {
                    writer.Write(content.Log_Logger);
                }
            }
        }
        internal sealed class Log_MessageParam : PatternLayoutConverter
        {
            protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
            {
                Log content = loggingEvent.MessageObject as Log;
                if (content != null)
                {
                    writer.Write(content.Log_Message);
                }
            }
        }

HomeController.cs

        public ActionResult Index()
        {

            DateTime time = DateTime.Now;
            log4net.ILog loger = log4net.LogManager.GetLogger("iNotes");
            test.Log msg = new Log(time, "1", "2", "3", "hhhhh");
            loger.Info(msg);
            return View();
        }

不知道哪里出了问题,数据库连接正常,sql语句也可以执行

sooke的主页 sooke | 初学一级 | 园豆:52
提问于:2018-05-28 11:45
< >
分享
所有回答(1)
0

自带的字段能记录到吗?

如果不能,检查一下AssemblyInfo.cs文件有没有关于log4net的配置:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)];

如果能,检查自定义的Layout类所在程序集是否跟项目程序集一致,不一致的话,需要在配置文件显式指定所在的程序集名:

<param name="Parameter"> <param name="ParameterName" value="@log_level" /> <param name="DbType" value="String" /> <param name="Size" value="50" /> <param name="Layout" type="test.MyLaout,程序集名称"> <param name="ConversionPattern" value="%log_level" /> </param> </param>

沃尔德 | 园豆:202 (菜鸟二级) | 2018-06-14 11:44

@沃尔德: 之前自带的字段是可以插到数据库的,改成自定义字段就不行了,assemblyInfo里也加了那行,程序集也是一致的

支持(0) 反对(0) sooke | 园豆:52 (初学一级) | 2018-06-14 12:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册