seq-extensions-logging icon indicating copy to clipboard operation
seq-extensions-logging copied to clipboard

Capture `ExpandoObject` as dictionaries/structures

Open omidkrad opened this issue 4 years ago • 5 comments

When logging an object that has a property of type Dictionary using @ then it's shown as expected in Seq like:

{
  DictionaryObj: {
    'ItemA': 100,
    'ItemB': 200,
  }
}

But when logging the dictionary object directly, it shows as a list of KeyValuePairs like:

[ItemA, 100], [ItemB, 200]

It is not consistent in how a Dictionary object is displayed and I highly prefer it to show as an object than a list, as in:

{
  'ItemA': 100,
  'ItemB': 200,
}

Update: dynamic objects which are dictionaries underneath already log as expected as objects. ExpandoObjects also need to be fixed to log as objects.

omidkrad avatar Jun 23 '21 18:06 omidkrad

Thanks for the suggestion! In addition to ExpandoObject, what are the concrete types of the objects you are expecting to show up as a dictionary? (A regular, generic Dictionary<K, V>?)

nblumhardt avatar Jun 23 '21 20:06 nblumhardt

I'm mostly looking for Dictionary<string, T> which is basically an associative array similar to JavaScript objects where the key is a string and the value can be a primitive or an object.

omidkrad avatar Jun 24 '21 05:06 omidkrad

Update: dynamic objects which are dictionaries underneath already log as expected as objects.

@omidkrad im facing the same issue, can you please provide some details how you got it working. when i wrap a dictionary into a dynamic it is logged as {}

JanEggers avatar Oct 14 '21 11:10 JanEggers

@JanEggers Try this helper method (I'm not sure if it worked!)

public static object ToDynamicObject<T>(IDictionary<string, T> source)
{
    dynamic eo = new ExpandoObject();
    var eoColl = (IDictionary<string, object>)eo!;
    foreach (var kvp in source)
    {
        eoColl.Add(kvp.Key, kvp.Value!);
    }
    return eo;
}

omidkrad avatar Oct 18 '21 14:10 omidkrad

@omidkrad thx but that didnt work for me either. I created a wrapper object like so:

public class ParamsWrapper : Dictionary<string, object>
        {
            public ParamsWrapper(IDictionary<string, object> source)
                : base(source)
            {
                
            }
        }

and logged it with @

that way i at least see the properties of complex objects. but they are still logged as array of key/value pairs instead of as object

JanEggers avatar Oct 19 '21 07:10 JanEggers