-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MessageSequence's IEnumerable Handling Assumptions lead to erroneous serialization #157
Comments
thanks, i will check soon. |
First, there is no provision for customizing generic output, and we are not using JsonSerializer for that purpose. |
Json is not the only issue for me, it really has to do with how IEnumerables are treated by default. I think it is simply wrong to assume that all IEnumerables should be treated as collections for the purposes of logging (whether they use Json or not). To give you a concrete example, I have a type |
However, if it can't handle simple arrays, that would be unsatisfactory in its own way. |
I thought about it, but from a consistency standpoint, it might seem like unnecessary meddling. I'll think about it a bit more. |
I think that perhaps my 2nd suggestion would be preferable. Simply adding one or more cases for |
Such additions that are meant to address only one's own specific cases are not desirable at all. |
One can argue the same way for |
This means that ISpanFormattable is supported, but IEnumerable is given priority. |
This is just an assumption until you are hit by one of those cases. I think that since Logging is more to do with serialization/formatting, if a type is both IEnumerable and ISpanFormattable, ISpanFormattable should be given priority since it "knows how to format" and in fact is the reason for the interface in the first place. |
Since no one likes unnecessary breaking changes, I've kept the basic behavior the same while prioritizing IFormattable/ISpanFormattable when dealing with IEnumerable, just as you suggested. |
Thank you for supporting IFormattable. However there is also a secondary issue which I mentioned too: IEnumerables get passed as plain IEnumerable and not as actual type when they go Json route: Again this is problematic for cases where a type defines its own Json serialization logic. So perhaps they should instead be passed as The general point here is that if the logging ends up going via Json serialization route, then it should properly handle Json serialization as defined by the types themselves, not relying on general assumptions simply by looking at the most common interfaces. |
Ah, since this is Boxed, it's correct to pass the Type. |
The following cases in the
MessageSequence.ToString
methods are very problematic:https://github.com/Cysharp/ZLogger/blob/b373ebbf67a41eb54ec283b4945785b29d2f7bca/src/ZLogger/ZLoggerInterpolatedStringHandler.cs#L255C1-L258C26
https://github.com/Cysharp/ZLogger/blob/b373ebbf67a41eb54ec283b4945785b29d2f7bca/src/ZLogger/ZLoggerInterpolatedStringHandler.cs#L294C1-L298C26
JsonSerializer
which may not be desired behavior in all cases.IEnumerable
and not as actual type. This is really problematic because:IEnumerable<T>
s:IEnumerable<Foo>
andIEnumerable<Bar>
, and its non-genericIEnumerable
version may not be applicable at all.IEnumerable<T>
is a "normal" collection - it may have inherited the interface, or the interface may be there for technical reasons, but should not be considered for serialization purposes).JsonConverterAttribute
that actually specifies how it should be serialized. This routine completely disregards it.So, to that end:
IEnumerable
case should perhaps be removed altogether, andToString
be used insteadIFormattable
orISpanFormattable
cases should be added - types checked against those, and these interfaces used for stringification.IEnumerable
, such that serializer can pick upJsonConverterAttribute
.The text was updated successfully, but these errors were encountered: