3

I am inserting JSON format data in sql database using log4net. Everything is fine except custom properties which are not being saved.

This is my configuration:

<appender name="TGGADONetAppenderjson" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,       Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=LOANER-1122-HP\SQLEXPRESS;Initial Catalog=CAS-Dev;integrated security=false;persist security info=True;User Id=sa;Password=abinash12345;" />
      <commandText value="INSERT INTO Log ([Message],[AppName],[TransactionId]) VALUES
       (@message, @appName,@transactionId)" />

 <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json"></layout>
      </parameter>
<parameter>
        <parameterName value="@appName" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <!--<conversionPattern value="%property{Environment}" />-->
          <conversionPattern value="APPNAME-LogTest" />
          <!--should be a fixed value-->
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@transactionId" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.PatternLayout" >
          <conversionPattern value="%property{TransactionId}" />
        </layout>
      </parameter>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="TGGADONetAppenderjson" />
    </root>

When I debug, I can see the values of the custom property "TransactionId" is being put into the thread. However the insert does not work. The TransactionId is not logged in the message nor inserted into the column TransactionId.

I am using log4net json version 1.2.13.29 from NuGet

stuartd
  • 70,509
  • 14
  • 132
  • 163
Abi P
  • 1,410
  • 3
  • 22
  • 36
  • Here is the log if it helps. {"date":"2015-04-17T12:58:52.7092871-04:00","level":"INFO","appname":"LogginTest-Console.vshost.exe","logger":"METHODNAME - LogTest.Main","thread":"6","ndc":"(null)","message":"Calling method Divide by zero. start"} – Abi P Apr 18 '15 at 22:10
  • 1
    Does it log in a text file? Whatever the answer is you can try activating internal debugging for the log4net assembly: https://logging.apache.org/log4net/release/faq.html#internalDebug – samy Apr 20 '15 at 07:02
  • Hi, I was able to figureout the issue with my code which was causing the TransactionId not appearing in the database. However the main issue is the transactionId is not paret of the json. Its coming as a separate field. How can I make that part of the json? – Abi P Apr 22 '15 at 15:54

1 Answers1

1

There's some more configuration to go into the SerializedLayout. You need to explicitly name the property. Try something like:

<layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
    <decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
    <default /> <!-- explicit default members -->
    <member value='TransactionId' /> <!-- explicit property reference -->
</layout> 

Check the Members test case that might help you.

Alternatively, serialize all the properties by adding a properties member.

Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42