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

Allow replacing IExternalScopeProvider via DI #50590

Closed
shirhatti opened this issue Apr 1, 2021 · 12 comments
Closed

Allow replacing IExternalScopeProvider via DI #50590

shirhatti opened this issue Apr 1, 2021 · 12 comments
Labels
api-approved API was approved in API review, it can be implemented area-Extensions-Logging enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@shirhatti
Copy link
Contributor

shirhatti commented Apr 1, 2021

Copied from #52153 created by @Sundow:

Background and Motivation

Currently Microsoft.Extensions.Logging.LoggerFactory class always instantiates IExternalScopeProvider with a default implementation. #50590 proposes to enable dependency injection so it is possible to use a custom implementation. This requires to add a constructor with one more parameter of type IExternalScopeProvider to be used by DI engine.

Proposed API

public partial class LoggerFactory : Microsoft.Extensions.Logging.ILoggerFactory
{
    public LoggerFactory(System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider> providers, Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.LoggerFilterOptions> filterOption, Microsoft.Extensions.Options.IOptions<Microsoft.Extensions.Logging.LoggerFactoryOptions> options = null, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider = null) { }
}

Usage Examples

Will be implicitly used by DI engine.

Risks

Low, as no existing interfaces are changed and only a trivial field assignment is required for a new one.

Other notes

The actual implementation of the proposal already exists in #52132 and can be looked at there, but requires API change approval.


Original description by @shirhatti:

Original Description

Today the default LoggerFactory implementation instantiates it's own IExternalScopeProvider. We should resolve it from DI instead allowing folks to replace the default IExternalScopeProvider.

@shirhatti shirhatti added enhancement Product code improvement that does NOT require public API changes/additions area-Extensions-Logging labels Apr 1, 2021
@ghost
Copy link

ghost commented Apr 1, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Today the default LoggerFactory implementation instantiates it's own IExternalScopeProvider. We should resolve it from DI instead allowing folks to replace the default IExternalScopeProvider.

Author: shirhatti
Assignees: -
Labels:

area-Extensions-Logging, enhancement

Milestone: -

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 1, 2021
@davidfowl
Copy link
Member

It might make sense to link the previous issues

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Apr 4, 2021
@maryamariyan maryamariyan added this to the 6.0.0 milestone Apr 4, 2021
@maryamariyan maryamariyan added the help wanted [up-for-grabs] Good issue for external contributors label Apr 4, 2021
@Sundow
Copy link
Contributor

Sundow commented Apr 28, 2021

Interested to work on it. Can you assign?

Would it be a reasonable approach if I try to get IExternalScopeProvider from DI and if not successful fall back to instantiating IExternalScopeProvider explicitly as it is now?

@KalleOlaviNiemitalo
Copy link

I thought LoggingServiceCollectionExtensions.AddLogging would be able to register LoggerExternalScopeProvider like it registers the other services:

services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));

but apparently #37092 changed things so that LoggerFactory instantiates LoggerFactoryScopeProvider instead, and LoggerExternalScopeProvider is no longer used by anything other than tests. I think @Sundow's approach is OK then. And if an app provides its own IExternalScopeProvider, then LoggerFactoryOptions.ActivityTrackingOptions will have no effect by default.

Sundow pushed a commit to Sundow/runtime that referenced this issue Apr 29, 2021
LoggingServiceCollectionExtensions is modified to allow replacing of the default implementation of IExternalScopeProvider with dependency injected version.

Fix dotnet#50590
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Apr 29, 2021
Sundow pushed a commit to Sundow/runtime that referenced this issue Apr 30, 2021
Added LoggerFactory constructor with  IExternalScopeProvider parameter allowing to use injected scope provider implementation. If nothing is provided uses the default implementation as before.

Fix dotnet#50590
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Apr 30, 2021
@maryamariyan
Copy link
Member

maryamariyan commented Apr 30, 2021

The ask in this issue is to allow replacing the default IExternalScopeProvider.

The default one is set in

_scopeProvider = new LoggerFactoryScopeProvider(_factoryOptions.ActivityTrackingOptions);

Do we need new API proposed in order to allow injecting IExternalScopeProvider here? (if yes, an API suggestion available here: #52132)

API Suggestion:

public partial class LoggerFactory : Microsoft.Extensions.Logging.ILoggerFactory
{
    public LoggerFactory(System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.ILoggerProvider> providers, Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.LoggerFilterOptions> filterOption, Microsoft.Extensions.Options.IOptions<Microsoft.Extensions.Logging.LoggerFactoryOptions> options = null, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider = null) { }
}

cc: @tarekgh

@maryamariyan maryamariyan added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed help wanted [up-for-grabs] Good issue for external contributors labels Apr 30, 2021
@maryamariyan
Copy link
Member

cc: @Sundow (the linked PR author) who prototyped this proposal. Are you interested in preparing a proposal following instructions in api-review-process.md?

Would be good to add usage examples for the proposed API to show how the proposal addresses the issue.

@Sundow
Copy link
Contributor

Sundow commented Apr 30, 2021

cc: @maryamariyan Yes, I'll try. Thanks for your comments, it's really helpful.

@maryamariyan maryamariyan added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels May 3, 2021
@bartonjs
Copy link
Member

bartonjs commented May 27, 2021

Video

The existing longest constructor needs to have its default removed to follow the guidance for how to add new defaulted parameters safely.

namespace Microsoft.Extensions.Logging
{
    public partial class LoggerFactory : Microsoft.Extensions.Logging.ILoggerFactory
    {
         public LoggerFactory(
             IEnumerable<ILoggerProvider> providers,
             IOptionsMonitor<LoggerFilterOptions> filterOption,
-            IOptions<LoggerFactoryOptions> options = null)
+            IOptions<LoggerFactoryOptions> options) { }

+        public LoggerFactory(
+            IEnumerable<ILoggerProvider> providers,
+            IOptionsMonitor<LoggerFilterOptions> filterOption,
+            LoggerFactoryOptions> options = null,
+            IExternalScopeProvider scopeProvider = null) { }

        }
    }
}

@bartonjs bartonjs removed the api-ready-for-review API is ready for review, it is NOT ready for implementation label May 27, 2021
@bartonjs bartonjs added the api-approved API was approved in API review, it can be implemented label May 27, 2021
Sundow pushed a commit to Sundow/runtime that referenced this issue May 28, 2021
Added LoggerFactory constructor with  IExternalScopeProvider parameter allowing to use injected scope provider implementation. If nothing is provided uses the default implementation as before.

Fix dotnet#50590
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 28, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 23, 2021
@maryamariyan maryamariyan modified the milestones: 6.0.0, 7.0.0 Jul 23, 2021
@danmoseley
Copy link
Member

@maryamariyan should this be marked up for grabs?

@maryamariyan maryamariyan added the help wanted [up-for-grabs] Good issue for external contributors label Mar 10, 2022
@maryamariyan
Copy link
Member

marked up for grabs

@heathbm
Copy link
Contributor

heathbm commented Apr 4, 2022

I have some time and would like to contribute to this.

@eerhardt
Copy link
Member

Resolved by #67520

@ghost ghost locked as resolved and limited conversation to collaborators Aug 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-Extensions-Logging enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
9 participants