Skip to content

Sample on how to use logger scope is either not explained very well or incomplete #32520

@PonchoPowers

Description

@PonchoPowers

Description

The example I'm referring to is:

[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
{
    TodoItem todoItem;
    var transactionId = Guid.NewGuid().ToString();
    using (_logger.BeginScope(new List<KeyValuePair<string, object>>
        {
            new KeyValuePair<string, object>("TransactionId", transactionId),
        }))
    {
        _logger.LogInformation(MyLogEvents.GetItem, "Getting item {Id}", id);

        todoItem = await _context.TodoItems.FindAsync(id);

        if (todoItem == null)
        {
            _logger.LogWarning(MyLogEvents.GetItemNotFound, 
                "Get({Id}) NOT FOUND", id);
            return NotFound();
        }
    }

    return ItemToDTO(todoItem);
}

How is TransactionId used in the example? There is no explanation as to what would would be logged as a result of adding it either, giving the impression that the following is logged:

"Getting item X"

Could either the example be updated to show how TransactionId would be used, or provide clarity how TransactionId is used if it is used at all in the example?

Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/logging/index.md

Document ID

726e3bf1-f367-d733-8933-bccc04da0e16

Article author

@tdykstra

Metadata

Metadata

Assignees

Type

No type

Projects

Status

In progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions