Serilog: Natively support JSON.NET dynamic objects (JObject)

Created on 2 May 2013  ·  13Comments  ·  Source: serilog/serilog

On Twitter you mentioned that it should be fairly straightforward to implement via IDestructuringPolicy
;)

enhancement

Most helpful comment

All 13 comments

I was trying to get this working via IDestructuringPolicy but, I'm afraid it won't work.

Cursory look through the codebase seems to indicate (here https://github.com/serilog/serilog/blob/master/src/Serilog/Parameters/PropertyValueConverter.cs?source=c#L112) that PropertyValueConverterfirst treats the objects as a collection (all JTokens are IEnumerable) and therefore never bothers checking with IDestructuringPolicy.

I _think_ perhaps giving the IDestructuringPolicy a higher priority (checking with it first, and then falling back to the default behaviour) might offer a way out, but I may be completeley wrong, I only spent 2 minutes looking at this code.

I created a quick workaround for now by... adding a level of indirection.

I added a class called JObjectWrapper that is not IEnumerable and I wrap my JObject with it. Now my IDestructuringPolicy gets called and the world is a happy place again.

@kkozmic how's this one turning out? Worth us removing the limitation you mention above? Any other thoughts? Cheers :)

well, it's a nasty workaround :) It gets me where I need to be but I'd rather have a more elegant solution than that if possible.

New interface, perhaps?

public interface ISequenceConversionPolicy
{
   bool TryConvertToSequence(object value, ILogEventPropertyValueFactory propertyValueFactory, out SequenceValue result);
}

Also, maybe:

public interface IDictionaryConversionPolicy

:)

Closing for now, but open to working through this one if anyone's keen to try a PR. Thanks for all the input folks! :)

@kkozmic Any chance I could take a look at your IDestructuringPolicy (the one that works with the wrapper)?

Sorry, I don't have that code with me anymore.

On Tue Feb 03 2015 at 4:38:45 AM Harry McIntyre [email protected]
wrote:

@kkozmic https://github.com/kkozmic Any chance I could take a look at
your IDestructuringPolicy (the one that works with the wrapper)?


Reply to this email directly or view it on GitHub
https://github.com/serilog/serilog/issues/30#issuecomment-72500600.

371 echoes this one, I think it's one for us to reopen, but might be a "2.0"

Build 1.4.168 removes the limitation that made this awkward; would love to see a _Serilog.Extras.JObject_ or similar that takes advantage of it :-)

Thanks @Jaben for nailing this!

Nice!

@nblumhardt that's awesome, thanks!

Was this page helpful?
0 / 5 - 0 ratings