Repository navigation
Add Microsoft.Testing.Extensions.Logging bridge to Microsoft.Extensions.Logging - #8344
Merged
Amaury Levé (Evangelink) merged 2 commits intoMay 19, 2026
Merged
Conversation
…ns.Logging
A new opt-in side-package that lets users forward Microsoft.Testing.Platform's
diagnostic log messages to any Microsoft.Extensions.Logging provider (Console,
Debug, Serilog, OpenTelemetry, Application Insights, ...). MTP's homegrown
logging stays in core (zero deps, AOT-friendly, netstandard2.0); the bridge is
purely additive and never referenced by core or by existing extensions.
RFC 013 (`docs/RFCs/013-Microsoft-Extensions-Bridges.md`) codifies the policy
and outlines future phases (Configuration, DI, Hosting).
Public API (`[TPEXP]`):
ITestApplicationBuilder.AddMicrosoftExtensionsLogging(Action<ILoggingBuilder>)
ITestApplicationBuilder.AddMicrosoftExtensionsLogging(ILoggerFactory)
The first overload owns its MEL `LoggerFactory`; the second forwards to a
caller-owned factory. Both short-circuit to a no-op when MTP's effective
`LogLevel` is `None` (no `--diagnostic`), so expensive sinks are not
constructed for runs that will never emit a log.
Tightly coupled platform fix included: MTP's `LoggerFactory` was never
disposed at host shutdown, so buffered MEL providers (Serilog, App Insights,
OpenTelemetry) would silently lose their final flush. `LoggerFactoryProxy`
now implements `IDisposable`/`IAsyncDisposable` and is disposed explicitly
at the end of `CommonHost.RunAsync`'s finally block, after the explicit
`FileLoggerProvider` disposal, so user-registered logger providers flush
last.
Tests added:
- 6 acceptance tests (2 x 3 TFMs) verifying forwarding, short-circuit, and
end-of-process flush.
- 4 unit tests for `LoggerFactoryProxy`'s new disposal surface
(idempotency, `ObjectDisposedException` on post-dispose `CreateLogger`).
- 8 unit tests for `LogLevelMapper` (exhaustive `DataRow` + future-proofing
test).
Reviewed across three expert-review rounds: 21/21 dimensions clean.
`Build.cmd -pack` succeeds with 0 warnings, 0 errors.
Co-authored-by: Copilot <[email protected]>
Amaury Levé (Evangelink)
left a comment
Member
Author
There was a problem hiding this comment.
Review of PR #8344 — Microsoft.Testing.Extensions.Logging bridge
| # | Dimension | Verdict |
|---|---|---|
| 1 | Algorithmic Correctness | ✅ LGTM |
| 2 | Threading & Concurrency | ✅ LGTM |
| 3 | Security & IPC | ✅ N/A |
| 4 | Public API & Binary Compat | ✅ LGTM |
| 5 | Performance & Allocations | ✅ LGTM |
| 6 | Cross-TFM Compatibility | ✅ LGTM |
| 7 | Resource & IDisposable | 🟡 1 NIT (see inline) |
| 8 | Defensive Coding | ✅ LGTM |
| 9 | Localization | ✅ LGTM |
| 10 | Test Isolation | ✅ LGTM |
| 11 | Assertion Quality | ✅ LGTM |
| 12 | Flakiness Patterns | ✅ LGTM |
| 13 | Test Completeness | ✅ LGTM |
| 14 | Data-Driven Coverage | ✅ LGTM |
| 15 | Code Style | ✅ LGTM |
| 16 | Naming & Conventions | ✅ LGTM |
| 17 | Documentation | ✅ LGTM |
| 18 | Analyzer Quality | ✅ N/A |
| 19 | IPC Wire Compatibility | ✅ N/A |
| 20 | Build Infrastructure | ✅ LGTM |
| 21 | Scope & PR Discipline | ✅ LGTM |
✅ 20/21 dimensions clean.
- NIT (
MicrosoftExtensionsLoggingProvider.csline 33):CreateLoggerdoes not guard against post-dispose calls, unlike the symmetricalLoggerFactoryProxy.CreateLogger. See inline comment. Not blocking — unreachable in the current host shutdown ordering — but worth closing for consistency.
Generated by Expert Code Review (on open) for issue #8344 · ● 9.2M
- Guard MicrosoftExtensionsLoggingProvider.CreateLogger against post-dispose calls (mirrors LoggerFactoryProxy.CreateLogger). - Register Microsoft.Testing.Extensions.Logging in NonWindowsTests.slnf so the package is produced on Linux/macOS CI and the acceptance-test ExtractVersionFromPackage lookup succeeds. Co-authored-by: Copilot <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a new opt-in side-package,
Microsoft.Testing.Extensions.Logging, that lets users forward Microsoft.Testing.Platform's diagnostic log messages to anyMicrosoft.Extensions.Loggingprovider — Console, Debug, Serilog, OpenTelemetry, Application Insights, custom sinks, and so on. MTP's homegrownMicrosoft.Testing.Platform.Loggingstays in core (zero dependencies, AOT-friendly, netstandard2.0); the bridge is purely additive and is never referenced by core or by any existing extension.This is phase 1 of the policy laid out in the new RFC 013 — Microsoft.Extensions.* Bridges. Future phases (Configuration, DI, Hosting) will follow the same shape.
Public API ([TPEXP])
The first overload owns and disposes the MEL
LoggerFactory; the second forwards to a caller-owned factory. Both short-circuit to a no-op when MTP's effectiveLogLevelisNone(no--diagnostic), so expensive sinks (Application Insights connection setup, Serilog file sinks, OTLP exporters, …) are not constructed for runs that will never emit a log.Usage:
Semantic mapping
LogLevel.Trace…Critical/None(0..6)Microsoft.Extensions.Logging.LogLevelswitchfor safety/AOTLog<TState>(level, state, ex, formatter)Log<TState>(level, EventId.None, state, ex, formatter)EventIddefaults toNoneLogAsync<TState>(...)Log+Task.CompletedTaskIsEnabled(level)LoggerFactory.Dispose/DisposeAsyncTightly coupled platform fix
Discovered via the rubber-duck and expert-reviewer passes: MTP's
LoggerFactorywas never disposed during host shutdown, so user-registered MEL providers like Serilog / Application Insights / OpenTelemetry would silently lose their final flush. The fix:LoggerFactoryProxynow implementsIDisposable(+IAsyncDisposableonNETCOREAPP), forwarding to the wrapped factory with thread-safe idempotency (Interlocked.Exchangeon_disposed).CreateLoggerafterDisposethrowsObjectDisposedExceptioninstead of silently dispatching.CommonHost.RunAsync'sfinallyblock now disposes the proxy explicitly, after the explicitFileLoggerProviderdisposal, so user-registered logger providers flush last. The proxy is skipped by the standardDisposeServiceProviderAsyncloop (same pattern asFileLoggerProvider) so ordering is preserved.LoggerFactory.Disposegot a// NOTE:comment flagging that, post-PR, this method is now reachable at runtime so future authors notice when adding new sharedILoggerProviderregistrations.Tests (all passing)
MicrosoftExtensionsLoggingTests, 2 × 3 TFMs): forwarding works, short-circuit works (asserts the configure delegate is not invoked when--diagnosticis off), and the bridge-ownedLoggerFactoryis disposed at end of process so buffered MEL providers flush.LoggerFactoryProxy's disposal surface: idempotency,ObjectDisposedExceptionpost-dispose,IAsyncDisposablepreference. 25/25 pass on net8/net9/net462.LogLevelMapper: exhaustiveDataRowover all 7 levels + a future-proofingEnum.GetValuestest that catches silent fallback toNoneif a new MTPLogLevelis ever added without updating the mapper. 32/32 pass on net8/net9/net462/net472.DiagnosticTestsstill green (FileLoggerProviderlifecycle preserved).Review
LoggerFactorydisposal before any code was committed, driving the platform-side fix.Build.cmd -packsucceeds with 0 warnings, 0 errors. All 23 NuGet packages produced and content-validated, including the newMicrosoft.Testing.Extensions.Logging.1.0.0-dev.nupkg.Intentional non-goals (deferred to future phases)
ILoggerFactoryexposed asMicrosoft.Extensions.Logging.ILoggerFactory) — omitted from v1 to avoid speculative public API.--diagnosticfile logger; both run together when--diagnosticis enabled.configure, so theTestingPlatformBuilderHookpattern does not apply.Files
New:
docs/RFCs/013-Microsoft-Extensions-Bridges.mdsrc/Platform/Microsoft.Testing.Extensions.Logging/(csproj, BannedSymbols, PACKAGE.md, PublicAPI, 4 internal classes + 2 public extension methods)test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/MicrosoftExtensionsLoggingTests.cstest/UnitTests/Microsoft.Testing.Extensions.UnitTests/LogLevelMapperTests.csModified:
Directory.Packages.props— addedMicrosoft.Extensions.Logging+Microsoft.Extensions.Logging.Console(the latter only consumed bysamples/Playground).TestFx.slnx,Microsoft.Testing.Platform.slnf— registered the new project.samples/Playground/*— opt-in example wiring the bridge withAddSimpleConsole.src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactoryProxy.cs— disposable + thread-safe idempotency.src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactory.cs— documentation note flagging new reachability.src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs— explicit proxy disposal.test/IntegrationTests/.../Helpers/AcceptanceTestBase.cs— extracts the new extension's alpha version.test/UnitTests/.../LoggerFactoryProxyTests.cs— +4 tests covering new disposal surface.test/UnitTests/Microsoft.Testing.Extensions.UnitTests/Microsoft.Testing.Extensions.UnitTests.csproj— references the bridge for unit testing.