Hi,
When using structured logging like :
_logger.Log('Data: {@data}', data);
This logs every property of data with extremely high depth, so I want to ignore a property from being serialized by attribute or something or limit the Max Depth of logger.
I tried [JsonIgnore] and [IgnoreDataMember] and excludeProperties on Target but they didn't work.
Sounds like a good idea to support IgnoreDataMember-attribute for properties. PR are welcome.
You can inject your own IJsonConverter at NLog.Config.ConfigurationItemFactory.Default.JsonConverter and do special handling for your special object:
See also: https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#i-like-to-use-jsonnet-for-creating-json
This way you can inject your own special converter that handles wanted types, or else just forwards the call to the defaultConverter:
var defaultConverter = NLog.Config.ConfigurationItemFactory.Default.JsonConverter;
var newConverter = new MyConverter(defaultConverter);
NLog.Config.ConfigurationItemFactory.Default.JsonConverter = newConverter;
Another quick option is to:
I assume your question has been answered, if not, please let us know!
What about doing this when using DI such as Autofac?
NLog 5.0 will make it easier to override the IJsonConverter by hooking up a dependency-injection-container. See also: #3666
NLog 4.7 will includes #3610 that will allow one to transform objects into anonymous-objects with only the wanted properties.
Most helpful comment
Sounds like a good idea to support
IgnoreDataMember-attribute for properties. PR are welcome.You can inject your own
IJsonConverteratNLog.Config.ConfigurationItemFactory.Default.JsonConverterand do special handling for your special object:See also: https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#i-like-to-use-jsonnet-for-creating-json
This way you can inject your own special converter that handles wanted types, or else just forwards the call to the
defaultConverter: