2014-07-15 87 views
0

在過去的幾個小時裏,我一直在試圖讓日誌寫入數據庫的表中。Log4net不會寫入數據庫

開啓調試後,我收到以下錯誤語句: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)); 

「輸出」窗口中未顯示任何錯誤。

任何幫助,將不勝感激。

+0

嘗試打開內部調試,看看會發生什麼:http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems –

+0

嘿,對於遲到的回覆抱歉,我已經變成了調試並收到以下錯誤:SqlCommand.Prepare方法要求所有可變長度參數有一個顯式設置非零大小 –

回答

0

你可以嘗試給所有的字符串參數的大小如下?大小應該與數據庫中的實際大小相匹配。

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

好吧,我向前邁進了一步,我需要設置我的小數的精度和比例,你能幫助嗎?與語法請? –

+0

一般對於固定大小的數據類型(int,decimal,bit等),你不需要指定大小 –

+0

嗯,我確實追加了規模和精度,它確實有效。現在我得到System.FormatException:無法將參數值從字符串轉換爲Int32。 ---> System.FormatException:輸入字符串的格式不正確。 –

0

我已經解決了這個問題,通過向我的小數添加精度和小數位數並將大小添加到字符串中。另外,我在查詢中錯過了一個冒號。 謝謝你們的支持!

相關問題