Table of Contents

Use log scopes to add data to related messages

public Response Get(Request request, Guid correlationId)
{
    using (logger.BeginScope(new List<KeyValuePair<string, object>>
        {
            new("CorrelationId", correlationId),
        }))
    {
        logger.LogInformation("Request started.");
        response = interactor.Get(request);
        logger.LogInformation("Request finished.");
        return response;
    }

The correlationId is added to all log messages that are created withing the scope. This is not limited to the Request started. and Request finished. messages. The correlationId will be added to every log entry that is create in the interactor, or any subsequent class.

Properties commonly added to the log scope are: Correlation Id, User Id, Request Id, Session Id, Environment.

References

© 2024 Rob van der Velden. All rights reserved.