Log4net不写入数据库

本文关键字:数据库 Log4net | 更新日期: 2023-09-27 18:13:15

在过去的几个小时里,我一直在尝试将日志写入数据库中的表。

打开调试开关后,我收到以下错误语句:SqlCommand. exePrepare方法要求所有可变长度参数都有一个显式设置的非零Size

下面是我的片段:

配置部分:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
    <root>
      <level value="ALL"/>
      <appender-ref ref="loginLog"/>
      <appender-ref ref="maskyooLog"/>
      <appender-ref ref="bpelLog"/>
    </root>
    <appender name="bpelLog" 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=***; uid=***; pwd=****; database=***" />
      <commandText value="INSERT INTO dbo.BPELLog ([UserName], [ProcessModeID], [CcCompanyNameID], [CcExpDate], 
                                                   [CcID], [CcOwnerID], [CcOwnerName], [InvoiceDate], [CustomerNo], 
                                                   [SumWithDue], [SumWithoutDue], [PaymentTypeID], [TaxRegistrationNum], [TrxUniqueID], [SalesPerson]) 
                          VALUES (@UserName, @ProcessModeID, @CcCompanyNameID, @CcExpDate, @CcID, @CcOwnerID, @CcOwnerName,
                                 @InvoiceDate, @CustomerNo, @SumWithDue, @SumWithoutDue, @PaymentTypeID, 
                                 @TaxRegistrationNum, @TrxUniqueID, @SalesPerson)" />
      <parameter>
        <parameterName value="@InvoiceDate" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@ProcessModeID" />
        <dbType value="Int32" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{ProcessModeID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@PaymentTypeID" />
        <dbType value="Int32" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{PaymentTypeID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CustomerNo"/>
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CustomerNo}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@TaxRegistrationNum"/>
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{TaxRegistrationNum}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@SalesPerson"/>
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{SalesPerson}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@TrxUniqueID"/>
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{TrxUniqueID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CcOwnerName" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CcOwnerName}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CcOwnerID" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CcOwnerID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CcID" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CcID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CcExpDate" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CcExpDate}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@UserName" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{UserName}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@CcCompanyNameID" />
        <dbType value="Int32" />
        <size value="15" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CcCompanyNameID}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@SumWithoutDue" />
        <dbType value="decimal" />
        <size value="20" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{SumWithoutDue}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@SumWithDue" />
        <dbType value="decimal" />
        <size value="20" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{SumWithDue}" />
        </layout>
      </parameter>
    </appender>

和类:

ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            log4net.Config.XmlConfigurator.Configure();
            log4net.Util.SystemInfo.NullText = null;
            GlobalContext.Properties["UserName"] = userName;
            GlobalContext.Properties["ProcessModeID"] = processModeId;
            GlobalContext.Properties["CcCompanyNameID"] = ccCompanyNameId;
            GlobalContext.Properties["CcExpDate"] = cCExpDate;
            GlobalContext.Properties["CcID"] = ccId;
            GlobalContext.Properties["CcOwnerID"] = ccOwnerId;
            GlobalContext.Properties["CcOwnerName"] = ccOwnerName;
            GlobalContext.Properties["InvoiceDate"] = invoiceDate;
            GlobalContext.Properties["CustomerNo"] = customerNo;
            GlobalContext.Properties["SumWithDue"] = sumWithDue;
            GlobalContext.Properties["SumWithoutDue"] = sumWithoutDue;
            GlobalContext.Properties["PaymentTypeID"] = paymentTypeId;
            GlobalContext.Properties["TaxRegistrationNum"] = taxRegistrationNum;
            GlobalContext.Properties["TrxUniqueID"] = trxUniqueId;
            GlobalContext.Properties["SalesPerson"] = salesPerson;
            log.Debug(string.Format("UserName: {0}, ProcessModeID: {1}, CcCompanyNameID: {2}, CcExpDate: {3}, CcID: {4}, CcOwnerID: {5}, InvoiceDate: {6}, CustomerNo: {7}, SumWithDue: {8}, SumWithoutDue: {9}, PaymentTypeID: {10},"
                                    + "TaxRegistrationNum: {11}, TrxUniqueID: {12}, SalesPerson: {13}", userName, processModeId, ccCompanyNameId, cCExpDate, ccId, ccOwnerId, ccOwnerName, invoiceDate, customerNo, sumWithDue, sumWithoutDue,
                                    paymentTypeId, taxRegistrationNum, trxUniqueId, salesPerson));

输出窗口中没有错误显示。

Log4net不写入数据库

你能试着给你的所有字符串参数的大小如下?该大小应该与数据库中的实际大小匹配。

     <parameter>
        <parameterName value="@CustomerNo"/>
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CustomerNo}" />
        </layout>
      </parameter>

我已经通过添加小数的精度和比例以及添加字符串的大小来解决这个问题。另外,我在查询中遗漏了一个冒号。感谢大家的支持!