Skip to content

Fix DynamicSerilog registration conflict when using AddSteeltoe#1660

Closed
TimHess wants to merge 1 commit intomainfrom
fix/1658-dynamic-serilog-bootstrap-ordering
Closed

Fix DynamicSerilog registration conflict when using AddSteeltoe#1660
TimHess wants to merge 1 commit intomainfrom
fix/1658-dynamic-serilog-bootstrap-ordering

Conversation

@TimHess
Copy link
Member

@TimHess TimHess commented Mar 12, 2026

Description

When both DynamicSerilog and Management.Endpoint assemblies are loaded, BootstrapScanner's WireAllActuators and WireDistributedTracingLogProcessor internally call AddDynamicConsole, which can register before the user's AddDynamicSerilog due to IHostBuilder callback ordering. This causes AddDynamicSerilog to throw because it finds an existing IDynamicLoggerProvider.

Fix by pre-registering DynamicSerilog within the same deferred callback as the actuator/tracing registration when the Serilog assembly is loaded. This ensures Serilog's IDynamicLoggerProvider is always present before AddDynamicConsole runs, which then gracefully skips.

Fixes #1658

Quality checklist

  • Your code complies with our Coding Style.
  • You've updated unit and/or integration tests for your change, where applicable.
  • You've updated documentation for your change, where applicable.
    If your change affects other repositories, such as Documentation, Samples and/or MainSite, add linked PRs here.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.
  • You've added required license files and/or file headers (explaining where the code came from with proper attribution), where code is copied from StackOverflow, a blog, or OSS.

When both DynamicSerilog and Management.Endpoint assemblies are loaded,
BootstrapScanner's WireAllActuators and WireDistributedTracingLogProcessor
internally call AddDynamicConsole, which can register before the user's
AddDynamicSerilog due to IHostBuilder callback ordering. This causes
AddDynamicSerilog to throw because it finds an existing IDynamicLoggerProvider.

Fix by pre-registering DynamicSerilog within the same deferred callback as
the actuator/tracing registration when the Serilog assembly is loaded. This
ensures Serilog's IDynamicLoggerProvider is always present before
AddDynamicConsole runs, which then gracefully skips.

Made-with: Cursor
@TimHess TimHess changed the title Fix DynamicSerilog registration conflict when using AddSteeltoe (#1658) Fix DynamicSerilog registration conflict when using AddSteeltoe Mar 12, 2026
@sonarqubecloud
Copy link

[InlineData(HostBuilderType.WebHost)]
[InlineData(HostBuilderType.WebApplication)]
[InlineData(HostBuilderType.HostApplication)]
public async Task DynamicSerilog_WinsOverActuatorFallback(HostBuilderType hostBuilderType)
Copy link
Member

Choose a reason for hiding this comment

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

This test doesn't seem to cover the reported problem. It succeeds even without the code changes.

@TimHess
Copy link
Member Author

TimHess commented Mar 13, 2026

Not only does the test not cover the reported problem, I can't actually reproduce it in the first place... I missed in the Cursor conversation that it made an assumption the bug was already confirmed

@TimHess TimHess closed this Mar 13, 2026
@bart-vmware bart-vmware deleted the fix/1658-dynamic-serilog-bootstrap-ordering branch March 13, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A different IDynamicLoggerProvider has already been registered

2 participants