Log4net不写入DB

时间:2014-07-15 16:52:35

标签: c# database log4net log4net-configuration

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

打开调试后,我收到以下错误声明:SqlCommand.Prepare方法要求所有变量长度参数都有明确设置的非零大小

以下是我的摘要:

配置部分:

<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));

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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以尝试给出所有字符串参数的大小,如下所示吗?大小应与数据库中的实际大小相匹配。

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

答案 1 :(得分:0)

我通过将精度和比例添加到我的小数并向字符串添加大小来解决问题。另外,我在查询中错过了一个冒号。 谢谢你们的支持!