2009-12-23 25 views
8

Sto utilizzando log4net con AdoNetAppender. Registra tutte le informazioni di registro in una tabella. Questa tabella ha in realtà 2 colonne Integer (può essere null).Valori predefiniti per il parametro AdoNetAppender

Ecco la parte rilevante della mia log4net config:

<commandText value="INSERT INTO ActivityLog ([Date],[Thread],[Level],[Logger],[Message],[DealID]) 
       VALUES (@log_date,@thread,@log_level,@logger,@message,@DealID)" /> 

//other parameters hten DealID 
<parameter> 
     <parameterName value="@DealID" /> 
     <dbType value="Int32" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%property{DealID}" /> 
     </layout> 
    </parameter> 

Quello che ho scoperto è stata se non esplicitamente impostato usando qualcosa come log4net.ThreadContext.Properties["DealID"] = DealID; mi genera un'eccezione:

System.FormatException occurred 
    Message="Failed to convert parameter value from a String to a Int32." 
    Source="System.Data" 
    StackTrace: 
     at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) 
    InnerException: System.FormatException 
     Message="Input string was not in a correct format." 
     Source="mscorlib" 
     StackTrace: 
      at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
      at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 
      at System.String.System.IConvertible.ToInt32(IFormatProvider provider) 
      at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 
      at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) 
     InnerException: 

Dovrei impostare come:

log4net.ThreadContext.Properties["DealID"] = 0;

Esiste comunque la possibilità di impostare un valore di parametro predefinito nella configurazione di log4net per questo campo Int32 in modo che non sia necessario impostarlo esplicitamente su 0 se non viene fornito alcun valore? E mi chiedo perché non succeda ai campi che sono impostati come varchar (anche se non viene loro fornito alcun valore).

risposta

27

Cambia il tuo appender:

<parameter> 
    <parameterName value="@DealID" /> 
    <dbType value="Int32" /> 
    <layout type="log4net.Layout.RawPropertyLayout"> <!-- notice this --> 
     <key value="DealID" /> <!-- and notice this instead of the pattern layout --> 
    </layout> 
</parameter> 

E a dare credito, ho trovato from this thread. (E un sacco di altre ricerche che cercano di fare la stessa cosa.

Problemi correlati