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

Use structured logging in logs instead of string interpolation #49

Open
Arithmomaniac opened this issue Jul 6, 2021 · 0 comments
Open

Comments

@Arithmomaniac
Copy link

Two related points:

To ensure that a message template is correctly logged and properties are transferred as custom properties, you always need to log properties with placeholders in the correct order, e.g.
logger.LogWarning("The person {PersonId} could not be found.", personId);
Never use string interpolation because the replacement will be done in your application and the logging backend has no longer access to the message template and individual custom properties:
logger.LogWarning($"The person {personId} could not be found.");
Another downside of string interpolation is that objects are always serialized into text in your app even if the log is not actually written because there is a log level filter configured.

Together, this would turn

logger.Debug($"Executing activity #{State.NumCompletedActivities} '{currentActivity.GetType().Name}'...");

into something like
logger.LogDebug($"Executing activity #{activityNumber} '{currentActivityName}'...", State.NumCompletedActivities, currentActivity.GetType().Name)

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

1 participant