0

I am trying to programmatically overwrite the CommandText property of log4net.Appender.AdoNetAppender. When I debug it, the property seems like it is set, but the the Insertion never terminates.

The CommandText value itself is surely correct. If I paste it directly into the Appender's config file, it inserts the record. I use multiple, other type of appenders, and those also do their job.

How should I overwrite the CommandText property at runtime?

public class DdsAdoNetAppender : AdoNetAppender
    {
        public new string ConnectionString
        {
            get
            {
                return base.ConnectionString;
            }
            set
            {
               CommandText = "INSERT INTO ApplicationLogs ([LogDateTime],[LogType],[MachineName],[UserName],[LogMessage],[LogException]) " +
                    "VALUES (@log_date_time, @log_type, @machine_name, @user_name, @log_message, @log_exception)";
            }
        }
    }

My Appender config: (The value of commandText is basically empty.)

<appender name="AdoNetAppender" type="DigitalDrawingStore.Backend.Logging.AdoNetAppenders.DdsAdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <ReconnectOnError value="true" />
      <connectionString value="data source = (localdb)\\MSSQLLocalDB; initial catalog = TestAdoNetLogDb; integrated security = true" />
      <commandText value="" />
      <commandType value="Text" />

      <parameter>
        <parameterName value="@log_date_time" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
      </parameter>
      <parameter>
        <parameterName value="@log_type" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout" value="%property{LogType}" />
      </parameter>
      <parameter>
        <parameterName value="@machine_name" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%property{MachineName}" />
      </parameter>
      <parameter>
        <parameterName value="@user_name" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%property{UserName}" />
      </parameter>
      <parameter>
        <parameterName value="@log_message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout" value="%property{LogMessage}" />
      </parameter>
       <parameter>
        <parameterName value="@log_exception" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout" value="%property{LogException}" />
      </parameter>
    </appender>

2 Answers2

1

The documentation says you can change the value via the property. I assume you can add a constructor, call the base one, then change commandtext. Alternatively, it says you can override GetLogStatement.

https://logging.apache.org/log4net/release/sdk/html/P_log4net_Appender_AdoNetAppender_CommandText.htm

  • Thanks for the answer, i tried with GetLogStatement overriding, and with constructor, but eventually removing these empty lines from the config file gave me the solution: `` and `` – Martin Markus Jun 29 '19 at 17:31
0

Eventually I have found the solution: I had too fully remove the following lines from the AdoNetAppender config:

<connectionString value="" />
<commandText value="" />