datalust / seq-extensions-logging

Add centralized log collection to ASP.NET Core apps with one line of code.
https://datalust.co/seq
Apache License 2.0
84 stars 12 forks source link

Logging a Dictionary object #47

Open omidkrad opened 3 years ago

omidkrad commented 3 years ago

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.

nblumhardt commented 3 years ago

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

omidkrad commented 3 years ago

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.

JanEggers commented 2 years ago

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

omidkrad commented 2 years ago

@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;
}
JanEggers commented 2 years ago

@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