Skip to content

Use structured logging in logs instead of string interpolation #49

@Arithmomaniac

Description

@Arithmomaniac

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions