Skip to content

Remove default logging scope from ASP.NET Core #44873

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Nov 4, 2022

  • We log the activity information by default, the span id is more useful than the traceidentifier.
  • Add the path and trace id as activity tags

I was looking at this and wondering why we still need it other than compatibility. I'm thinking we can lean into activity (as big as it is 😄) and use it's scope instead of having 2 by default. This reduces the number of async locals as well which is a plus. @noahfalk do you think it's reasonable to have a feature for logging adding tags to the logging activity scope as well?

cc @noahfalk @tarekgh @adityamandaleeka

These are the per request allocations (excluding execution context overhead) (10K requests):

Type Allocations Bytes Average Size (Bytes)
| - Microsoft.Extensions.Logging.Logger.Scope 10,125 486,000 48
| - Microsoft.Extensions.Logging.LoggerFactoryScopeProvider.Scope 10,125 486,000 48
| - Microsoft.AspNetCore.Hosting.HostingApplicationDiagnostics.Log.HostingLogScope 10,000 400,000 40
| - System.IDisposable[] 10,125 324,000 32

@ghost ghost added the area-runtime label Nov 4, 2022
@tarekgh
Copy link
Member

tarekgh commented Nov 4, 2022

Add the path and trace id as activity tags

Did you mean Request Id instead of trace id?

@tarekgh
Copy link
Member

tarekgh commented Nov 4, 2022

@davidfowl I think this is change is good if we are not concering about the compat. If we proceed with such change would be good to file breaking change doc for that.

CC @reyang @cijothomas

@noahfalk
Copy link
Member

noahfalk commented Nov 4, 2022

@noahfalk do you think it's reasonable to have a feature for logging adding tags to the logging activity scope as well?

what if we encourage people to phase out usage of logger scope and use Activity instead? My worry if we try to automatically migrate tags is that the scope lifetimes may not match up and you could have weird results. For example:

// this might be ASP.Net code
void HandleRequest()
{
    using (Activity a = source.StartActivity)
    {
        UserRequestHandler(requestData);
    }
}

void UserRequestHandler(requestData)
{
    foreach(var item in requestData.Batch)
    {
        // this scope doesn't have 1:1 relationship with Activity, the tags would overwrite each other
        using(logger.CreateScope("Id", item.Id))
        {
            logger.Log("stuff");
        }
        // this log is outside the logger scope, but still inside the Activity scope where
        // the tags have now been forwarded.
        logger.Log("all done now");
    }
}

@NinoFloris
Copy link

Relevant #31028

@davidfowl
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@amcasey
Copy link
Member

amcasey commented Apr 15, 2023

@davidfowl What's happening with this?

@davidfowl davidfowl marked this pull request as ready for review July 22, 2023 17:01
- We correlate logs with Activities and more information can be added to them (like the trace identifier) using activity listeners or custom middleware.
@davidfowl davidfowl force-pushed the davidfowl/remove-logging-scopes branch from 579ac6b to 0bc04d3 Compare July 23, 2023 13:10
@davidfowl davidfowl added the breaking-change This issue / pr will introduce a breaking change, when resolved / merged. label Jul 23, 2023
@ghost ghost added the needs-breaking-change-announcement Indicates that breaking change announcement shuold be posted and linked to this PR label Jul 23, 2023
@ghost
Copy link

ghost commented Jul 23, 2023

Thanks for identifying a breaking change.

, after you commit this PR please take the following actions, as part of the breaking changes announcement process:
\n- [ ] Create an announcement issue by using the ASP.NET Core breaking change issue template.
\n- [ ] Link the breaking change announcement issue from this PR.
\n- [ ] Remove the needs-breaking-change-announcement label.

@davidfowl
Copy link
Member Author

@noahfalk I updated this PR to just remove the scopes.

I'm pretty convinced this scope useful by default and can be easily added back via middleware. The cost we're paying for the benefit people get by default is too high (many don't even know how to look at these logging scopes). I think now that the ability to associate logs with the activity exists and is on by default. I feel pretty confident breaking this.

We will show people how to add a middleware back and adds this information via a logging scope or activity tags.

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 1, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 20, 2024
@danmoseley danmoseley requested a review from Copilot February 14, 2025 04:10
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

src/Hosting/Hosting/test/WebHostTests.cs:871

  • The removal of the WebHost_CreatesDefaultRequestIdentifierFeature_IfNotPresent test case reduces the test coverage. Ensure that the new behavior is adequately tested.
[Fact]

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs:95

  • Ensure that the removal of the Scope property and its associated logic is covered by tests to verify that the activity's scope is used correctly.
if (loggingEnabled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions breaking-change This issue / pr will introduce a breaking change, when resolved / merged. needs-breaking-change-announcement Indicates that breaking change announcement shuold be posted and linked to this PR pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants