Skip to content
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

Is there a way to tell RenderedCompactJsonFormatter or to not render some placeholders? #68

Open
james-hu opened this issue Nov 5, 2024 · 2 comments

Comments

@james-hu
Copy link

james-hu commented Nov 5, 2024

I have this use case:

  • There are multiple variables I want to included in logging
  • For most of them, they are strings or numbers, so that I am happy for them to be rendered in the message
  • Some of them are huge objects, so that I want it to be not rendered in the message, but still attached as a destructured property of the output

This is the example code with "$" showing the use case:

_logger.LogDebug("Created a Job {jobId} for {customer}. Details: {$job}", jobId, customerName, jobDetails);

For CompactJsonFormatter and RenderedCompactJsonFormatter, the output does not contain destructured details of jobDetails in the property "job". So that it is not very useful to me.

This is the example code with "@" showing the use case:

_logger.LogDebug("Created a Job {jobId} for {customer}. Details: {@jobDetails}", jobId, customerName, jobDetails);

For both CompactJsonFormatter and RenderedCompactJsonFormatter, the output contains destructured details of jobDetails in the property "job".
For RenderedCompactJsonFormatter, the rendered message contains fully destructured details of jobDetails too, so that the same information gets duplicated. Is there a way to tell RenderedCompactJsonFormatter not to render it in the message?

@nblumhardt
Copy link
Member

nblumhardt commented Nov 8, 2024

This should be closer to what you want:

_logger
    .ForContext(LogEventLevel.Debug, "jobDetails", jobDetails, destructureObjects: true)
    .Debug("Created a Job {jobId} for {customer}. Details: {$job}", jobId, customerName, jobDetails);

Edit: this uses the Serilog ILogger API; should be adaptable to MEL's ILogger with a bit more code.

@james-hu
Copy link
Author

james-hu commented Nov 8, 2024

Yes, this should work, I didn't think of this approach ☺️ Thanks!
Based on this approach, I think an extension can be created as a sugar for making the code easier to write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants