Iian
Iian

Reputation: 305

log4net.ext.json: logging just the message property

I am using log4net.ext.json to serialize the 'message' property to my logs. However, when I use the standard configuration, the 'message' object is included as a property on another object.

Here is my configuration

<parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
      <decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
      <default /><!--explicit default members-->
      <remove value='message' /><!--remove the default preformatted message member-->
      <member value='message:messageobject' /><!--add raw message-->
    </layout>
  </parameter>

The data that is being logged, however, doesn't just log the message object, it includes other context information such as appname, level, and exception.

This is a problem when an exception is logged as the added stack trace text sometimes exceeds the maximum field size in the database.

date": "2016-05-20T09:06:18.6288814+10:00",
"level": "WARN",
"appname": "/LM/W3SVC/3/ROOT-1-131081726390968061",
"logger": "DefaultLogger",
"thread": "10",
"ndc": "(null)",
-"message": {
    "Message": "Model validation errors",
    "Method": "LogValidationErrorsAttribute.OnActionExecuting",
    "IsLocal": true,
    "IsAuthenticated": false,
    "IsSecureConnection": false,
    "HttpMethod": "POST",        
    "CurrentUICulture": "en",
    "CurrentCulture": "en",
}

All I want to do is log just the 'message' property of the logging event object in this particular parameter.

Basically, it should look like this in the log:

{
    "Message": "Model validation errors",
    "Method": "LogValidationErrorsAttribute.OnActionExecuting",
    "IsLocal": true,
    "IsAuthenticated": false,
    "IsSecureConnection": false,
    "HttpMethod": "POST",        
    "CurrentUICulture": "en",
    "CurrentCulture": "en",
}

Any ideas how to do this?

Upvotes: 1

Views: 1946

Answers (1)

ladenedge
ladenedge

Reputation: 13429

Remove the <default/> element. It tells the layout to include all of the default properties in the event.

Note that the property name ("message") will remain, however:

{
  "message": {
    "Message": "Model validation errors",
    "Method": "LogValidationErrorsAttribute.OnActionExecuting",
    ...
  }
}

Upvotes: 1

Related Questions