datalust/seq-extensions-logging

Capture `ExpandoObject` as dictionaries/structures

omidkrad opened this issue · 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.

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>?)

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.

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 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 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