How to log Byte[] data in log4net using AdoNetAppender

674 Views Asked by At

I want to log a gzip-compressed string, which will be in binary Byte[] format, in a [varbinary](max) SQL table column using log4net's AdoNetAppender.

My questions:

1) Is this possible? If so, what dbType I need to use as parameter in my config file below:

   <parameter>
    <parameterName value="@Data" />
    <dbType value="String" />
    <size value="2147483647" />
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="Data" />
    </layout>
  </parameter

2) Are there alternatives other than converting the value to a string and then logging it.

2

There are 2 best solutions below

0
On BEST ANSWER

I think the best way to log you binary Byte[] is to convert it to a base64 string. There is no way to pass a Byte[] type as value to log4net logging methods.

0
On

You can convert your string to Byte[] in your CommandText like this:

<commandText value="INSERT INTO [LogServer]
    ([Date], [Logger], [message], [Exception], [BinaryMessage], [BinaryException]) 
    VALUES
    (@log_date, @logger, @message, @exception, cast(@message as varbinary(max)), cast(@exception as varbinary(max)) )"/>