问题
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
回答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.
来源:https://stackoverflow.com/questions/29723417/log4net-json-custom-properties-not-being-read