Skip to content

[11.0.100-preview.4.26224.122] GlobalToolsNETCore21Part02 execute "dotnet vstest" failed with “Moq.MockException : Expected invocation on the mock at least once, but was never performed” #127496

Closed as not planned

Description

@WeiweiCaiAcpt

Description

Prior to dotnet-sdk-11.0.100-preview.4.26224.122, running "dotnet vstest DockerWatch.Test.dll" resulted in a System.TypeLoadException when using dotnet-sdk-11.0.100-preview.4.26210.111 and dotnet-sdk-11.0.100-preview.4.26214.103 (refer to: [dotnet-sdk-11.0.100-preview.4.26214.103] GlobalToolsNETCore21Part02 execute "dotnet vstest" failed with error: System.TypeLoadException : Could not load type 'Microsoft.Extensions.Logging.Internal.FormattedLogValues'... · Issue #126995 · dotnet/runtime).
Starting from dotnet-sdk-11.0.100-preview.4.26224.122, the System.TypeLoadException no longer occurs; however, it is replaced by a new Moq.MockException, as shown below:

Moq.MockException :

Expected invocation on the mock at least once, but was never performed: m => m.Log<Object>(LogLevel.Trace, It.IsAny<EventId>(), It.Is<FormattedLogValues>(v => v.ToString().Contains("trace")), It.IsAny<Exception>(), It.IsAny<Func<Object, Exception, String>>())
No setups configured.

Performed invocations:
ILogger.Log<FormattedLogValues>(LogLevel.Trace, 0, [[{OriginalFormat}, trace]], null, Func<FormattedLogValues, Exception, String>)
  Stack Trace:
     at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage)
   at Moq.Mock.Verify[T](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
   at Moq.Mock`1.Verify(Expression`1 expression)
   at DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogTraceMessages() in C:\Users\v-joyzhu\Desktop\unit test\docker-watch-master\docker-watch-master\test\DockerWatch.Test\LoggerAdapterTest.cs:line 63
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

More detailed information, please see the devdiv bug.

Reproduction Steps

Environment Configuration:
The machine has dotnet-sdk-11.0.0-preview.4.26224.122 installed.

  1. Copy GlobalToolsNETCore21Part02Test56 from shared server (see location from devdiv bug) to local machine.
  2. Modify DockerWatch.Test.runtimeconfig.json to enable the app to run on dotnet-sdk-11.0.0-preview.4.26224.122.
    "framework": {
    "name": "Microsoft.NETCore.App",
    "version": "11.0.0-preview.4.26224.122"
    }

Repro Steps:

  1. Open cmd in GlobalToolsNETCore21Part02Test56.
  2. Execute command: dotnet vstest DockerWatch.Test.dll.

Expected behavior

Passed! - Failed: 0, Passed: 18, Skipped: 0, Total: 18, Duration: 3 s - DockerWatch.Test.dll

Actual behavior

3 test cases failed with below error:
[xUnit.net 00:00:03.70] DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogTraceMessages [FAIL]
[xUnit.net 00:00:03.71] DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnErrorMessage [FAIL]
[xUnit.net 00:00:03.72] DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnInformationMessage [FAIL]
Failed DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogTraceMessages [94 ms]
Error Message:
Moq.MockException :
Expected invocation on the mock at least once, but was never performed: m => m.Log(LogLevel.Trace, It.IsAny(), It.Is(v => v.ToString().Contains("trace")), It.IsAny(), It.IsAny<Func<Object, Exception, String>>())
No setups configured.

Performed invocations:
ILogger.Log(LogLevel.Trace, 0, [[{OriginalFormat}, trace]], null, Func<FormattedLogValues, Exception, String>)
Stack Trace:
at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage)
at Moq.Mock.Verify[T](Mock1 mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock1.Verify(Expression1 expression)
at DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogTraceMessages() in C:\Users\v-joyzhu\Desktop\unit test\docker-watch-master\docker-watch-master\test\DockerWatch.Test\LoggerAdapterTest.cs:line 63
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Failed DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnErrorMessage [12 ms]
Error Message:
Moq.MockException :
Expected invocation on the mock at least once, but was never performed: m => m.Log(LogLevel.Error, It.IsAny(), It.Is(v => v.ToString().Contains("error")), It.IsAny(), It.IsAny<Func<Object, Exception, String>>())
No setups configured.

Performed invocations:
ILogger.Log(LogLevel.Error, 0, [[{OriginalFormat}, error]], null, Func<FormattedLogValues, Exception, String>)
Stack Trace:
at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage)
at Moq.Mock.Verify[T](Mock1 mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock1.Verify(Expression1 expression)
at DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnErrorMessage() in C:\Users\v-joyzhu\Desktop\unit test\docker-watch-master\docker-watch-master\test\DockerWatch.Test\LoggerAdapterTest.cs:line 43
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Failed DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnInformationMessage [8 ms]
Error Message:
Moq.MockException :
Expected invocation on the mock at least once, but was never performed: m => m.Log(LogLevel.Information, It.IsAny(), It.Is(v => v.ToString().Contains("information")), It.IsAny(), It.IsAny<Func<Object, Exception, String>>())
No setups configured.

Performed invocations:
ILogger.Log(LogLevel.Information, 0, [[{OriginalFormat}, information]], null, Func<FormattedLogValues, Exception, String>)
Stack Trace:
at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage)
at Moq.Mock.Verify[T](Mock1 mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock1.Verify(Expression1 expression)
at DockerWatch.Test.LoggerAdapterTest.LogInformation_ShouldLogAnInformationMessage() in C:\Users\v-joyzhu\Desktop\unit test\docker-watch-master\docker-watch-master\test\DockerWatch.Test\LoggerAdapterTest.cs:line 23
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Failed! - Failed: 3, Passed: 15, Skipped: 0, Total: 18, Duration: 3 s - DockerWatch.Test.dll (netcoreapp2.1)

Regression?

Yes.

Verify Scenarios:
1). Windows 11 24H2 AMD64 + dotnet-sdk-11.0.100-preview.3.26207.106: Pass
2). Windows 11 24H2 AMD64 + dotnet-sdk-11.0.100-preview.4.26210.111: Fail (failed with System.TypeLoadException)
3). Windows 11 24H2 AMD64 + dotnet-sdk-11.0.100-preview.4.26214.103: Fail (failed with System.TypeLoadException)
4). Windows 11 24H2 AMD64 + dotnet-sdk-11.0.100-preview.4.26224.122: Fail (failed with Moq.MockException)

Known Workarounds

No response

Configuration

Application Name: GlobalToolsNETCore21Part02
OS: Windows 11 25H2
CPU: X64
.NET Build Number: dotnet-sdk-11.0.100-preview.4.26224.122

dotnet --info

.NET SDK:
Version: 11.0.100-preview.4.26224.122
Commit: 48bd87d899
Workload version: 11.0.100-manifests.e88583fc
MSBuild version: 18.7.0-preview-26224-122+48bd87d89

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\11.0.100-preview.4.26224.122\

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 11.0.0-preview.4.26224.122
Architecture: x64
Commit: 48bd87d899

.NET SDKs installed:
11.0.100-preview.4.26224.122 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 11.0.0-preview.4.26224.122 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 11.0.0-preview.4.26224.122 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 11.0.0-preview.4.26224.122 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other information

@dotnet-actwx-bot @dotnet/compat

Activity

  1. added
    untriaged
    on Apr 28, 2026
  2. dotnet-policy-service commented on Apr 28, 2026

    @dotnet-policy-service
    Contributor

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

  3. MihuBot commented on Apr 28, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  4. WeiweiCaiAcpt commented on Apr 28, 2026

    @WeiweiCaiAcpt
    Author

    @ericstj Could you please take a look at this bug? The previous System.TypeLoadException is no longer occurring, but a new Moq.MockException appears in the latest build (11.0.100-preview.4.26224.122).

    Also, could you please confirm whether this is a blocker for .NET 11 P4 validation? Thanks.

  5. cincuranet commented on Apr 28, 2026

    @cincuranet
    Contributor

    AI Analysis

    Context

    This is the follow-up to #126995 (closed by PR #127194, which restored an obsolete shim Microsoft.Extensions.Logging.Internal.FormattedLogValues in Microsoft.Extensions.Logging.Abstractions). With that shim in place, the original TypeLoadException no longer occurs — the test app (docker-watch LoggerAdapterTest) now actually runs to the Verify step, where Moq reports the expected m.Log(...) call was never observed.

    Root cause (this issue is not a runtime regression)

    The test was authored against Microsoft.Extensions.Logging v2.x. Its mock setup looks like the well-known Moq pattern:

    mockLogger.Verify(m => m.Log(
        LogLevel.Information,
        It.IsAny<EventId>(),
        It.IsAny<Microsoft.Extensions.Logging.Internal.FormattedLogValues>(),  // <- v2.x type
        null,
        It.IsAny<Func<Microsoft.Extensions.Logging.Internal.FormattedLogValues, Exception, string>>()));

    ILogger.Log<TState> is a generic method. Each unique TState produces a distinct method instantiation at runtime, and Moq's Verify matches on that exact instantiation.

    PR #127194 added Microsoft.Extensions.Logging.Internal.FormattedLogValues back as an [Obsolete(error: true)] [EditorBrowsable(Never)] shim — a different CLR type from the modern Microsoft.Extensions.Logging.FormattedLogValues that LoggerExtensions.LogInformation actually instantiates internally. So:

    • The test's reference to the old type resolves (TypeLoadException gone ✅)
    • But LoggerExtensions.LogInformation(...) calls logger.Log<Microsoft.Extensions.Logging.FormattedLogValues>(...) — the new namespace
    • The mock was set up against logger.Log<Microsoft.Extensions.Logging.Internal.FormattedLogValues>(...)
    • Different TState ⇒ different generic method instantiation ⇒ Moq sees zero matching invocations ⇒ MockException

    This is the canonical "mocking ILogger is fragile" problem — exactly the topic linked by MihuBot: dotnet/extensions#1319. It's not unique to .NET 11; it's a long-standing consequence of FormattedLogValues having been internalized in 3.0 (and was the very reason to mark these obsolete shim types error: true in PR #127194).

    Why the runtime can't round-trip this

    Restoring the shim cannot make it bind-equivalent to the modern type:

    • Two CLR types with the same simple name in different namespaces are always distinct identities.
    • Generic instantiations are keyed by exact type identity; the JIT cannot "unify" …Internal.FormattedLogValues and …FormattedLogValues.
    • Forcing LoggerExtensions to use the obsolete shim type internally would resurrect a public-but-obsolete type as a permanent ABI fixture in the modern code path — directly contradicting the intent of PR Add obsolete compatibility shims for moved Microsoft.Extensions.Logging internal types #127194 and the deprecation done years ago.

    Recommendation

    1. The test code pattern is the well-known Moq + ILogger anti-pattern. Recommended fix lives in test code, not the runtime.
    2. Standard workaround: verify against It.IsAnyType and stop depending on the internal FormattedLogValues identity:
      mockLogger.Verify(l => l.Log(
          LogLevel.Information,
          It.IsAny<EventId>(),
          It.Is<It.IsAnyType>((v, t) => true),
          It.IsAny<Exception?>(),
          It.IsAny<Func<It.IsAnyType, Exception?, string>>()));
    3. Better: use Microsoft.Extensions.Logging.Testing.FakeLogger (in Microsoft.Extensions.Diagnostics.Testing) instead of mocking ILogger.
  6. removed
    untriaged
    on Apr 28, 2026
  7. added this to the 11.0.0 milestone on Apr 28, 2026
  8. ericstj commented on Apr 28, 2026

    @ericstj
    Member

    Agree with @cincuranet's analysis and recommendation. We can restore the old types, but we won't make the runtime use them, nor would we ever "promise" to keep that the same.

    Also, could you please confirm whether this is a blocker for .NET 11 P4 validation? Thanks.

    This is not a blocker.

  9. WeiweiCaiAcpt commented on Apr 29, 2026

    @WeiweiCaiAcpt
    Author

    @ericstj Is this issue also caused by this breaking change ?

  10. ericstj commented on Apr 29, 2026

    @ericstj
    Member

    This issue is caused by this breaking change from 7 years ago -- dotnet/extensions#1319. It's observed when the tests are forced to run on net11 which updates the long-unsupported packages used by those tests to the latest version. I did some work to try to smooth over some of those breaks, but I cannot smooth this one over. In this case if someone cares about running the tests in this abandoned/archived respository they will need to update those tests as @cincuranet suggests.

    If we have any repo in active development (and likely to actually retarget to net11) they are unlikely to be referencing these very old packages. I can measure this by looking at the usages of this type across a number of repos / internally.

  11. ericstj commented on Apr 29, 2026

    @ericstj
    Member

    FormattedLogValues Breaking Change Impact Analysis

    Issue: dotnet/runtime#127496
    Change: Microsoft.Extensions.Logging.Internal.FormattedLogValues was made internal
    Date: 2026-04-29

    Search Methodology

    GitHub code search: FormattedLogValues Microsoft.Extensions.Logging.Internal excluding source files, docs, and metadata (XML docs, API diffs, checked-in bin/packages, .txt/.md/.xml/.java dumps).

    Total code results (excluding docs/metadata): ~124 hits across GitHub

    Category Breakdown

    Category Count Notes
    Test files 80 (65%) Unit tests using Moq/mock verification with FormattedLogValues
    Copies of source 6 (5%) Forks/copies of the actual FormattedLogValues.cs source
    Production source 38 (31%) Custom loggers, adapters, extensions (excluding comment-only references)

    Production Source Analysis (deep-dive on 13 files)

    Runtime Break — expects FormattedLogValues from framework callbacks (2 repos)

    These cast/check the state parameter in ILogger.Log() callbacks. Since the framework no longer passes FormattedLogValues as state, the cast returns null or the type check fails at runtime — even without recompilation.

    Repo Last Push TFM Usage Breaking Pattern
    dotnetmentor/structured-log-lines-dotnet 2022-12 netstandard2.0 Reflection + type check on incoming state Cast on incoming state will fail
    RedBearSys/RedBear.LogDNA.Extensions.Logging 2022-01 netstandard2.0 Cast on incoming state parameter Cast on incoming state returns null

    Recompile-only Break — constructs FormattedLogValues directly (4 repos)

    These create new FormattedLogValues(...) themselves. Since the type was brought back as obsolete, existing binaries continue to work. Only breaks when recompiled (obsolete error).

    Repo Last Push TFM Usage Breaking Pattern
    trinodb/trino-csharp-client 2025-02 netstandard2.0 Direct construction Obsolete on recompile
    d1820/cosmos-query-manager 2024-12 (unknown) Direct construction Obsolete on recompile
    Courio-Dev/UnobtrusiveMultitenancy 2018-11 netcoreapp2.1 Direct construction Obsolete on recompile
    mediuscloud/nhibernatebymedius 2025-06 netcoreapp2.0;net461 Direct construction Obsolete on recompile

    Would NOT Break (4 repos - comment only)

    Repo Usage
    tbebekis/AspNetCore-CustomLoggingProvider Type name referenced in comment only
    tbebekis/Study-AspNetCore-9x-MVC-WebApi-EFCore Type name referenced in comment only
    Leandro-is/AP-Formateador Type name referenced in comment only
    adambajguz/Typin Type name referenced in comment only

    Inaccessible (microsoft/msrecruit-scheduling-engine, Azure/azure-functions-rabbitmq-extension)

    SAML-protected; could not fetch.

    Test File Analysis

    The 80 test hits follow a common pattern:

    Notable Test Repos

    Repo Last Push Notes
    Azure/azure-functions-rabbitmq-extension 2026-02 Active Microsoft project (inaccessible)
    imgbot/Imgbot 2025-01 Direct cast to FormattedLogValues
    rsm-hcd/RSM.HCD.CSharp 2024-12 Active
    BotBuilderCommunity/botbuilder-community-dotnet 2024-07 Community project
    Most others 2016-2022 Inactive/abandoned

    NuGet Package Analysis

    Of the ~34 unique production source repos, 8 publish NuGet packages. None would affect applications targeting .NET 11:

    Package Downloads Last Updated Latest TFM Logging Dep Would Break?
    RedBear.LogDNA.Extensions.Logging 72K 2019-07 netstandard2.0 Logging 2.2.0 Yes — cast as FormattedLogValues
    Typin 43K 2021-12 net5.0/net6.0/ns2.0/ns2.1 Logging.Debug 3.0.0 No — comment-only reference
    ReflectInsight.Extensions.Logging 23K 2018-12 net461 Abstractions 2.2.0 Yes — custom logger
    AzureFromTheTrenches.Commanding... 17K 2019-03 netstandard2.0 Abstractions 2.2.0 Yes — auditor
    Daenet.Common.Logging 16K 2017-09 netstandard2.0 Abstractions 2.0.0 Yes — LogManager
    StructuredLogLines 9K 2018-09 netstandard2.0 Logging 2.1.1 Yes — reflection
    StripeEntities 9K 2015-09 (unspecified) (none) Yes — custom logger
    Daenet.Common.Logging.EventHub 5K 2017-09 netstandard2.0 Logging 2.0.0 Yes — EventHub logger

    Key observations:

    • All breaking packages target netstandard2.0 or net461 — none target net6.0+
    • All depend on Logging.Abstractions v2.x or earlier — the era when FormattedLogValues was public
    • Every breaking package is abandoned — last updated between 2015 and 2019 (6-11 years ago)
    • Download counts are all-time totals and quite modest (largest is 72K all-time)
    • Typin (highest downloads targeting modern TFMs) only references FormattedLogValues in a comment — it would not break
    • None of these packages have been updated to support modern .NET TFMs, meaning their consumers are also on old frameworks

    Key Findings

    1. Framework Targeting Supports "Won't Upgrade" Theory

    Every production repo checked targets old frameworks:

    • netstandard2.0 (most common)
    • netcoreapp2.0, netcoreapp2.1
    • net461
    • Package references to Logging.Abstractions v1.x - v2.x

    None target net6.0+ or any modern TFM. These projects are built against the era when FormattedLogValues was public and would not be upgrading to .NET 11.

    2. Predominantly Tests (65% of code hits)

    The most common usage is in test code verifying ILogger.Log() calls via Moq, matching It.IsAny<FormattedLogValues>(). This is exactly the pattern reported in #127496.

    3. Production Usage Is Custom Logger Adapters

    The production code using FormattedLogValues is almost exclusively:

    • Custom ILogger implementations that cast state to FormattedLogValues
    • Logging adapters (Splunk, LogDNA, AppInsights, Redis, EventHub)
    • These are all targeting old TFMs and old package versions

    Conclusion

    Note: The type was brought back as obsolete, so binary compatibility is maintained. Existing binaries referencing FormattedLogValues still compile and load. The actual runtime break is behavioral: the framework no longer uses FormattedLogValues as the state in ILogger.Log() callbacks.

    The runtime impact is very low for anyone actually upgrading to .NET 11:

    1. ~124 code references exist across all of GitHub (excluding docs/metadata — a very small number)
    2. 65% are tests — Moq verifications using It.IsAny<FormattedLogValues>() that will fail at runtime (the exact scenario from [11.0.100-preview.4.26224.122] GlobalToolsNETCore21Part02 execute "dotnet vstest" failed with “Moq.MockException : Expected invocation on the mock at least once, but was never performed” #127496)
    3. Only 2 production repos have actual runtime breaks (casting incoming state to FormattedLogValues) — both target netstandard2.0 with old deps and haven't been updated since 2022
    4. 4 production repos construct FormattedLogValues directly — these only break on recompile (obsolete error), not at runtime
    5. All affected NuGet packages are abandoned (last updated 2015-2019) with modest all-time downloads

    This strongly supports the theory that the people most likely to encounter this breakage are not actually updating to new frameworks, and the usage is predominantly in test code.

  12. locked and limited conversation to collaborators on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions