The log4net.Layout.PatternLayout generates a string as output. The
IDbDataParameter cannot have its DbType property set to Int32 and its
Value property set to a String.

You can use the RawPropertyLayout to extract a property value in its
original object form, rather than as a string.

<param name="Parameter"> 
   <param name="ParameterName" value="@userID" /> 
   <param name="DbType" value="String" /> 
   <param name="Size" value="40" /> 
   <param name="Layout" type="log4net.Layout.RawPropertyLayout"> 
      <param name="Key" value="UserID" /> 
   </param>
</param>

You will have to store the UserID (as an int) in the properties map,
e.g.:

log4net.ThreadContext.Properties["UserID"] = userId;


If you want to write your own custom database appender you can use this
as a good starting point:


public sealed class FastDbAppender : IAppender, IOptionHandler
{
        private string m_name;
        private string m_connectionString;
        private SqlConnection m_dbConnection;

        public string Name
        {
                get { return m_name; }
                set { m_name = value; }
        }

        public string ConnectionString
        {
                get { return m_connectionString; }
                set { m_connectionString = value; }
        }

        public void ActivateOptions() 
        {
                m_dbConnection = new SqlConnection(m_connectionString);
                m_dbConnection.Open();
        }

        public void Close()
        {
                if (m_dbConnection != null)
                {
                        m_dbConnection.Close();
                }
        }

        public void DoAppend(LoggingEvent loggingEvent)
        {
                SqlCommand command = m_dbConnection.CreateCommand();
                command.CommandText = "INSERT INTO [LogTable]
([Time],[Logger],[Level],[Thread],[Message]) VALUES
(@Time,@Logger,@Level,@Thread,@Message)";

                command.Parameters.Add("@Time", loggingEvent.TimeStamp);
                command.Parameters.Add("@Logger",
loggingEvent.LoggerName);
                command.Parameters.Add("@Level",
loggingEvent.Level.Name);
                command.Parameters.Add("@Thread",
loggingEvent.ThreadName);
                command.Parameters.Add("@Message",
loggingEvent.RenderedMessage);

                command.ExecuteNonQuery();
        }
}


Cheers,

Nicko


> -----Original Message-----
> From: pato43 [mailto:[EMAIL PROTECTED] 
> Sent: 11 July 2005 23:20
> To: [email protected]
> Subject: Type int in ADOAppender.
> 
> Dear Friends, 
> 
>   I really tried several times to make my ADOApeender to 
> accept a parameter as int, but did not work any time. 
> 
> The field in my database is an Int. My database is a sqlserver. 
> If i change the database type to string, works, when i back 
> to Int, doens't work, and any error appears. 
> 
> This kind of configuration works ok.... 
> <param name="Parameter"> 
>    <param name="ParameterName" value="@userID" /> 
>    <param name="DbType" value="String" /> 
>    <param name="Size" value="40" /> 
>    <param name="Layout" type="log4net.Layout.PatternLayout"> 
>       <param name="ConversionPattern" value="%x" /> 
>    </param>
> </param> 
> 
> But what i should to do to work with an Int ? 
> I try to change the DbType to Int, Int32. 
> I try to remove the size parameter and mantain the Dbtype as string. 
> I try to remove the layout parameter... and much more... :)) 
> 
> Please, help me. I'm going to write my own database logger. 
> 
> Regards, 
> 
> Pietro. 
> 

Reply via email to