Skip to content

fix: keep the log handler set by the brokerage test project - #9640

Merged
Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Romazes:feature-keep-brokerage-test-log-handler
Jul 28, 2026
Merged

fix: keep the log handler set by the brokerage test project#9640
Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Romazes:feature-keep-brokerage-test-log-handler

Conversation

@Romazes

@Romazes Romazes commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Description

BrokerageTests.Setup replaced Log.LogHandler with a new NUnitLogHandler before every test. That line is removed, so the log handler chosen by the test project is kept.

Related Issue

N/A

Motivation and Context

No fixture in Lean derives from BrokerageTests. Its consumers are the brokerage plugin repositories (Lean.Brokerages.Tastytrade, Lean.Brokerages.CharlesSchwab, Lean.Brokerages.Alpaca and others), which use it as the base class for their live brokerage tests. Each of them sets a log handler in its own TestSetup:

Log.LogHandler = new CompositeLogHandler(); // ConsoleLogHandler + FileLogHandler

That line runs once, from a TestCaseSource while NUnit builds the test tree, and never runs again. The first brokerage test replaced it, so for the rest of the run there was no log.txt and no console output. The only place left was TestContext.Progress, which the default dotnet test console logger does not print. A failing brokerage test in CI showed no log at all.

Why ConsoleLogHandler is the better default here

Both handlers write the same text. They differ in where the text goes and who can read it there.

ConsoleLogHandler NUnitLogHandler
Visible in Visual Studio yes, while the test runs yes, while the test runs
Visible in CI (dotnet test) yes, when the test fails no, never
Usable outside tests (Launcher, live trading) yes no, it lives in the test project and needs NUnit
Line order Error goes to stderr, the rest to stdout, so error lines can move one stream for all levels, order is kept
Save only errors to a file yes, 2> errors.txt no, everything is mixed

The order difference is real but small. Three logged lines:

TRACE:: sending order
ERROR:: order rejected
TRACE:: closing socket

With ConsoleLogHandler the error line travels on a different stream, so it can arrive last and look like the socket broke first. NUnitLogHandler keeps the order.

The CI difference is the one that costs something. With NUnitLogHandler only, a failing test in GitHub Actions produces an empty log. NUnitLogHandler is tidier, ConsoleLogHandler reaches more places, and the test project is the right place to make that choice.

Consumers that set no handler keep Lean's default ConsoleLogHandler instead of getting an NUnitLogHandler. Their output is still shown in Visual Studio while the test runs, and now it is also shown in CI when a test fails.

Requires Documentation Change

No

How Has This Been Tested?

  • Measured with a small NUnit project that the default dotnet test console logger prints Console.Out for a failing test, but never prints TestContext.Progress. This is why the handler from TestSetup has to survive.
  • Confirmed that TestSetup in the brokerage plugin repositories sets the log handler once, before any [SetUp] runs.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

@Romazes
Romazes requested a review from Martin-Molinero July 27, 2026 18:44
@Romazes Romazes self-assigned this Jul 27, 2026
- stop replacing it with NUnitLogHandler on every test
- brokerage test projects keep their file log for the whole run
@Romazes
Romazes force-pushed the feature-keep-brokerage-test-log-handler branch from b5147f6 to a2705df Compare July 27, 2026 19:44
@Martin-Molinero
Martin-Molinero merged commit 11ba019 into QuantConnect:master Jul 28, 2026
7 of 8 checks passed
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.

2 participants