Skip to content

Log QueryLayer and LINQ expression at Debug level #1732

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

Merged
merged 2 commits into from
May 25, 2025
Merged

Conversation

bkoelman
Copy link
Member

@bkoelman bkoelman commented May 25, 2025

The LINQ expression is logged using the AgileObjects.ReadableExpressions NuGet package. This is a light-up dependency, so it needs to be added explicitly to your project.

Example usage:

  1. dotnet add package AgileObjects.ReadableExpressions

  2. Enable debug logging in appsettings.json: "Logging:LogLevel:JsonApiDotNetCore.Repositories": "Debug"

  3. Send HTTP request: GET /api/todoItems?include=owner,assignee,tags&filter=equals(priority,'High') HTTP/1.1

Output:

dbug: JsonApiDotNetCore.Repositories.EntityFrameworkCoreRepository[1436092168]
      QueryLayer: QueryLayer<TodoItem>
      {
        Include: assignee,owner,tags
        Filter: equals(priority,'High')
        Sort: priority,-modifiedAt
        Pagination: Page number: 1, size: 10
        Selection
        {
          FieldSelectors<TodoItem>
          {
            tags: QueryLayer<Tag>
            {
              Sort: id
              Pagination: Page number: 1, size: 10
            }
            owner: QueryLayer<Person>
            {
            }
            assignee: QueryLayer<Person>
            {
            }
          }
        }
      }

dbug: JsonApiDotNetCore.Repositories.EntityFrameworkCoreRepository[1122238841]
      Expression tree: [Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
          .AsNoTrackingWithIdentityResolution()
          .Include("Tags")
          .Include("Owner")
          .Include("Assignee")
          .Where(todoItem => todoItem.Priority == value)
          .OrderBy(todoItem => todoItem.Priority)
          .ThenByDescending(todoItem => todoItem.LastModifiedAt)
          .Take(value)
          .Select(
              todoItem => new TodoItem
              {
                  Id = todoItem.Id,
                  CreatedAt = todoItem.CreatedAt,
                  Description = todoItem.Description,
                  DurationInHours = todoItem.DurationInHours,
                  LastModifiedAt = todoItem.LastModifiedAt,
                  Priority = todoItem.Priority,
                  Tags = todoItem.Tags
                      .OrderBy(tag => tag.Id)
                      .Take(value)
                      .ToHashSet(),
                  Owner = todoItem.Owner,
                  Assignee = todoItem.Assignee
              })

QUALITY CHECKLIST

@bkoelman bkoelman changed the title Log QueryLayer and Expression at Debug level Log QueryLayer and LINQ expression at Debug level May 25, 2025
Copy link

codecov bot commented May 25, 2025

Codecov Report

Attention: Patch coverage is 76.66667% with 7 lines in your changes missing coverage. Please review.

Project coverage is 90.76%. Comparing base (b6b56b0) to head (0ccfcc2).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...eries/QueryableBuilding/ExpressionTreeFormatter.cs 66.66% 2 Missing and 4 partials ⚠️
src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1732      +/-   ##
==========================================
- Coverage   90.78%   90.76%   -0.02%     
==========================================
  Files         467      468       +1     
  Lines       14616    14646      +30     
  Branches     2302     2308       +6     
==========================================
+ Hits        13269    13294      +25     
- Misses        917      918       +1     
- Partials      430      434       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bkoelman bkoelman marked this pull request as ready for review May 25, 2025 11:09
@bkoelman bkoelman merged commit 99c9a95 into master May 25, 2025
16 checks passed
@bkoelman bkoelman deleted the log-expressions branch May 25, 2025 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant