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

[API Proposal]: Expose NullExternalScopeProvider and NullScope #108993

Open
Foxtrek64 opened this issue Oct 17, 2024 · 2 comments
Open

[API Proposal]: Expose NullExternalScopeProvider and NullScope #108993

Foxtrek64 opened this issue Oct 17, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Logging
Milestone

Comments

@Foxtrek64
Copy link
Contributor

Foxtrek64 commented Oct 17, 2024

Background and motivation

When writing a custom logging provider, it's not uncommon to want to provide a default logging scope backed by NullExternalScopeProvider/NullScope. This provides a good default for log providers or situations that either don't require scope support or where a sane default is required and can be replaced later (e.g. by the logger provider implementing ISupportExternalScope).

API Proposal

NullExternalScopeProvider and NullScope should change their visibility from internal to public.

/// <summary>
/// An empty scope without any logic
/// </summary>
- internal sealed class NullScope : IDisposable
+ public sealed class NullScope : IDisposable
}
/// <summary>
/// Scope provider that does nothing.
/// </summary>
- internal sealed class NullExternalScopeProvider : IExternalScopeProvider
+ public sealed class NullExternalScopeProvider : IExternalScopeProvider

Because these types are specifically designed as no-op replacements, no additional changes or considerations are required.

API Usage

This example assumes that NullExternalScopeProvider.Instance is passed in the logger provider's CreateScope instance. It features support for later updating the scope provider should the logger provider implement ISupportExternalScope

public sealed class MyLogger(IExternalScopeProvider? scopeProvider)
{
    internal IExternalScopeProvider? ScopeProvider { get; set; } = scopeProvider;

    public IDisposable? BeginScope<TState>(TState state) where TState : notnull
        => ScopeProvider?.Push(state) ?? NullScope.Instance;

    // Remaining members excluded for brevity.
}

Alternative Designs

Currently, consumers of M.E.L that intend to write their own logger provider are required to implement their own no-op scope and scope provider types, should they wish for this functionality.

Risks

No expected risks. These no-op scope and scope provider types are agnostic to the ultimate destination of the logger.

@Foxtrek64 Foxtrek64 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 17, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 17, 2024
Copy link
Contributor

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

@Foxtrek64
Copy link
Contributor Author

Honorable mentions: #96128

@tarekgh tarekgh added this to the Future milestone Oct 17, 2024
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Logging
Projects
None yet
Development

No branches or pull requests

2 participants