使用Log4Net C#具有多个表的多个ADONetAppender

时间:2018-11-02 07:51:29

标签: c# logging log4net

enter image description here我正在尝试使用Log4Net使用自定义参数创建多个表。我能够创建多个表,正在插入自定义列和数据。但是似乎在两个表中都插入了意外的数据(作为参考的附加图像)。以下是我的App.config文件和代码。这是Log4Net的限制吗?请建议我我要去哪里了。

我对Log4Net有第二个问题。我需要具有许多自定义参数的日志记录应用程序。我试图将Log4Net纳入此要求,因为它因其性能优势而被广泛使用。但是我们需要为数据库中的单行条目编写多行代码。请提示Log4Net是否合适,还是我应该去构建Custom Logger应用程序

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>

    <appender name="ADONetAppender1" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=localhost;uid=xylemadmin;password=XylemInc@2018; database=LoggingLeafDB" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn])
                   VALUES (@log_date, @thread, @log_level, @logger, @message, @exception,@mynewcolumn)" />

      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <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="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@mynewcolumn"/>
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{mynewcolumn}" />
        </layout>
      </parameter>
    </appender>

    <appender name="ADONetAppender2" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=localhost;uid=xylemadmin;password=XylemInc@2018; database=LoggingLeafDB" />
      <commandText value="INSERT INTO MyLog ([Date],[Thread],[Level],[Message],[CustomColumn])
                   VALUES (@log_date, @thread,@log_level,@message,@customcolumn)" />

      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <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="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@customcolumn"/>
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{customcolumn}" />
        </layout>
      </parameter>
    </appender>

    <!--<appender name="ADONetAppender1" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=localhost;uid=xylemadmin;password=XylemInc@2018; database=LoggingLeafDB" />
      <commandText
        value="INSERT INTO Log ([AppName],[EventDate],[EventName],[LogLevel],[LogSource],[LogMessage],[Exception],[LogInfo1],[LogInfo2],[LogInfo3],[LogInfo4],[LogInfo5]) 
        VALUES (@appname, @eventdate, @eventname, @loglevel, @logsource, @logmessage,@exception,@loginfo1,@loginfo2,@loginfo3,@loginfo4,@loginfo5)" />
      <parameter>
        <parameterName value="@appname"/>
        <dbType value="String"/>
        <size value="100"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{appname}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@eventdate"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@eventname"/>
        <dbType value="String"/>
        <size value="100"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{eventname}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@loglevel"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logsource"/>
        <dbType value="String"/>
        <size value="100"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logmessage" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
      <parameter>
        <parameterName value="@loginfo1" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{loginfo1}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@loginfo2" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern  value="%property{loginfo2}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@loginfo3" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern  value="%property{loginfo3}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@loginfo4" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{loginfo4}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@loginfo5" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern  value="%property{loginfo5}" />
        </layout>
      </parameter>
    </appender>-->
    <root>
      <level value="ALL" />
      <appender-ref ref="ADONetAppender1" />
      <appender-ref ref="ADONetAppender2" />
    </root>
  </log4net>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>





class Program
    {

        private static readonly log4net.ILog log1 = log4net.LogManager.GetLogger("ADONetAppender1");
        private static readonly log4net.ILog log2 = log4net.LogManager.GetLogger("ADONetAppender2");
        static void Main(string[] args)
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();

                log4net.LogicalThreadContext.Properties["mynewcolumn"] = "Log table Custom column data";
                string Param1 = "Somedata1";
                string Param2 = "Somedata1";
                string Param3 = "Somedata1";
                log1.DebugFormat("Debug Format:  {0} , {1}  {2}", Param1, Param2, Param3);

                log4net.LogicalThreadContext.Properties["customcolumn"] = "MyLog table custom column data";

                string Err1 = "Error 1";
                string Err2 = "Error 2";

                log2.ErrorFormat("Debug Formam: {0} ,  {1}", Err1, Err2);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

    }

0 个答案:

没有答案