Skip to content
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

Improve nullable annotations for ILogger.BeginScope #66960

Merged
merged 3 commits into from
Mar 23, 2022

Conversation

maxkoshevoi
Copy link
Contributor

Based on this discussion: #66892 (comment)

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Mar 21, 2022
@ghost
Copy link

ghost commented Mar 21, 2022

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

Issue Details

Based on this discussion: #66892 (comment)

Author: maxkoshevoi
Assignees: -
Labels:

new-api-needs-documentation, area-Extensions-Logging, community-contribution

Milestone: -

@eerhardt
Copy link
Member

cc @davidfowl @lodejard @rynowak @BrennanConroy @halter73 - FYI, in case you have any feedback on this annotation. Specifically, the IDisposable? return type is what I am concerned about. But we already have 2 places that can return null from this interface method, so it does match reality.

public IDisposable BeginScope<TState>(TState state)
{
return _externalScopeProvider?.Push(state);
}

https://github.com/dotnet/aspnetcore/blob/17695facaeaf9d6d83aea9bfe87747180922117a/src/Logging.AzureAppServices/src/BatchingLogger.cs#L21-L24

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM - assuming no one else has any feedback.

Copy link
Member

@BrennanConroy BrennanConroy left a comment

Choose a reason for hiding this comment

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

Scopes aren't supported by all loggers so the IDisposable? makes sense to me.

@@ -20,7 +20,7 @@ private NullLogger()
}

/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state)
public IDisposable BeginScope<TState>(TState state) where TState : notnull
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, how does this not complain about hiding the nullable annotation on ILogger.BeginScope?

Copy link
Member

Choose a reason for hiding this comment

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

Your interface implementation can be more specific than the interface. Here the interface says "You can return an IDisposable, or null". And this implementation says "I only ever return an IDisposable", which is allowed.

The inverse isn't allowed though.

@eerhardt
Copy link
Member

I'm going to merge this. If anyone else has feedback, we can open a new PR to address it.

@eerhardt eerhardt merged commit ea4ebaa into dotnet:main Mar 23, 2022
@maxkoshevoi maxkoshevoi deleted the mk/fix-logging-abstractions branch March 23, 2022 21:31
radekdoulik pushed a commit to radekdoulik/runtime that referenced this pull request Mar 30, 2022
* BeginScope can return a null IDisposable. And TState is not nullable.
@ghost ghost locked as resolved and limited conversation to collaborators Apr 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Logging community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants