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

Introduce JsonSafeString #51

Merged
merged 5 commits into from
Aug 2, 2022
Merged

Conversation

nblumhardt
Copy link
Member

@nblumhardt nblumhardt commented Aug 2, 2022

Fixes #46

Since no serialization actually occurs, the PR proposes to do this without relying on the @ operator that the original ticket describes.

JsonSafeString can be passed anywhere that property values are accepted (including through BeginScope()).

Usage

var json = "{\"A\": 42}";
_logger.LogInformation("The answer is {Answer}", new JsonSafeString(json));

Result

image

@nblumhardt nblumhardt marked this pull request as ready for review August 2, 2022 04:32
@nblumhardt nblumhardt merged commit 5b3b7fa into datalust:dev Aug 2, 2022
@nblumhardt nblumhardt mentioned this pull request Aug 8, 2022
/// externally validated or proven to be valid JSON before calling this constructor.
/// </summary>
/// <param name="json">A well-formed JSON string that can be included directly in a log event.</param>
public JsonSafeString(string json)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO JsonSafeString should take a string? here. Since you're already checking for null in .ToString() it should be safe to pass a null string here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the note, @danbopes! In this case the intention's to communicate that the constructor is handed well-formed JSON, we're only really handling the null case in ToString() because it's impossible to prevent it in C#.

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

Successfully merging this pull request may close these issues.

Allow logging string as JSON object using the @ directive
3 participants