destructurama / attributed

Use attributes to control how complex types are logged to Serilog.
Apache License 2.0
269 stars 33 forks source link

Using LogWithNameAttribute will Prevent the Property From Being Destructured #65

Closed bowdens07 closed 8 months ago

bowdens07 commented 1 year ago

Suppose I'm logging a class that has another class inside of it, like so:

public class MessageBase
{
        [LogWithName("messageContext")]
        public ContextClass Context
}
public class ContextClass
{
 public string foo;
public string bar;
public override stringToString() {return "ContextClass ToString Output"}
}

var outputMessageBase = new MessageBase()
logger.logInfo("{@0}", outputMessageBase)

So we see MessageBase has a ContextClass inside of it. If I add the [LogWithName()] attribute over the Context property in MessageBase, serilog will no longer destructure MessageBase's Context property in the output, and will simply output Context's ToString() method. So here's the difference in output:

Without [LogWithName("messageContext")] on MessageBase's Context Property: {"ContextClass": {"foo":"", "bar":""}}

With [LogWithName("messageContext")] on MessageBase's Context Property: {"messageContext":"ContextClass ToString Output" }

While this is likely intended behavior, I would like some way to destructure the properties that use the [LogWithName()]. Ideally, I'd like a way for my output to be this: {"messageContext": {"foo":"", "bar":""}}

Perhaps an argument like a boolean could be passed to the [LogWithName()] attribute, so its API would now be [LogWithName(string newName, bool destructureObjects=false)]

Note - I am using Serilog.Formatting.Compact.CompactJsonFormatter going into a Console sink, although I don't think that changes the spirit of my question minus some minor details on precisely what the output format looks like. I'm just concerned with destructuring every property that uses destructurama attributes in my output.

Thank you for your time.

github-actions[bot] commented 9 months ago

This issue was marked as stale since it has not been active for a long time

github-actions[bot] commented 8 months ago

This issue was marked as stale since it has not been active for a long time

sungam3r commented 8 months ago

While this is likely intended behavior

I don't think so. I added destructureObjects: true in #110 and test.