当前位置: 代码迷 >> C# >> log4net写入数据库有关问题
  详细解决方案

log4net写入数据库有关问题

热度:89   发布时间:2016-05-05 03:34:19.0
log4net写入数据库问题

为什么我设置好数据库连接,插入的sql语句,当天也能写入数据,但是第二天的时候发送来的数据,控制台和日志文件都能收到,数据库就是没有写入进去,我手动写sql语句写入也是可以的,为什么log4net不能隔天写入数据库呢?
代码贴上

    class Program
    {
        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static void Main(string[] args)
        {
            // Log an info level message
            Console.WriteLine("Application [RemotingServer] Start");
            // Configure remoting. This loads the TCP channel as specified in the .config file.
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            // Publish the remote logging server. This is done using the log4net plugin.
            log4net.LogManager.GetRepository().PluginMap.Add(new log4net.Plugin.RemoteLoggingServerPlugin("LogOperator"));
            log4net.Config.XmlConfigurator.Configure();// 读取配置文件初始化log4Net
            // Wait for the user to exit
            Console.WriteLine("Press 0 and ENTER to Exit");
            String keyState = "";
            while (String.Compare(keyState, "0", true) != 0)
            {
                keyState = Console.ReadLine();
            }
            // Log an info level message
            if (log.IsInfoEnabled) log.Info("Application [RemotingServer] End");
        }
    }

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>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) [%ndc] - %message - account_id:%property{account_id} - role_id :%property{role_id} - action_type : %property{action_type}- %newline"/>
      </layout>
    </appender>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date account_id =&gt; %property{account_id} || role_id =&gt; %property{role_id} || role_name =&gt;  %property{role_name} || server_id =&gt;  %property{server_id} || action_type =&gt; %property{action_type} || action_value =&gt; %property{action_value} || extra_data =&gt; %property{extra_data} %newline"/>
      </layout>
    </appender>
    <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1"/>
      <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
      <param name="ConnectionString" value="server=localhost;database=xy2_web_transit;Uid=root;Pwd=123456;Charset=utf8;"/>
      <!--<commandText value="INSERT INTO xy2_game_log(log_datetime,log_account_id,log_role_id,log_role_name,log_server_id,log_action_type,log_action_value,log_extra_data) VALUES (@log_date,@account_id,@role_id,@role_name,@server_id,@action_type,@action_value,@extra_data)"/>-->
      <commandText value="call InsertGameLog (@log_date,@account_id,@role_id,@role_name,@server_id,@action_type,@action_value,@extra_data)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
        <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss}"/>
      </parameter>
      <parameter>
        <parameterName value="@account_id"/>
        <dbType value="String"/>
        <size value="32"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{account_id}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@role_id"/>
        <dbType value="String"/>
        <size value="32"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{role_id}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@server_id"/>
        <dbType value="String"/>
        <size value="32"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{server_id}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@action_type"/>
        <dbType value="String"/>
        <size value="10"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{action_type}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@action_value"/>
        <dbType value="String"/>
        <size value="500"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{action_value}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@extra_data"/>
        <dbType value="String"/>
        <size value="1500"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{extra_data}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@role_name"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{role_name}"/>
        </layout>
      </parameter>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="AdoNetAppender_MySql"/>
      <appender-ref ref="LogFileAppender"/>
    </root>
  </log4net>
  <system.runtime.remoting>
    <application name="Log4netRemotingServer">
      <!-- We need to define the remoting channels on which we will publish
           the remote logging sink. -->
      <channels>
        <channel displayName="Server Channel" ref="tcp server" port="8085"/>
      </channels>
    </application>
  </system.runtime.remoting>
  <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.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </DbProviderFactories>
  </system.data>
</configuration>


------解决思路----------------------
http://blog.csdn.net/duanzi_peng/article/details/9113611