Repository navigation
.NET: Use source-generated logging in Foundry Hosting - #10122
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The six new logging classes change legacy EventId metadata, potentially affecting filtering and grouping; preserve it or document the intentional change and assert numeric IDs and names in parity tests.
1 open issue
What changed in this PR
Migrates Foundry Hosting’s .NET logging to source-generated methods, reducing runtime logging overhead and aligning with the repository’s established pattern.
Changes:
- Replaces 29 direct logging calls with six component-local logging classes.
- Removes manual level checks where generated methods provide guards.
- Extends the recording logger and adds focused tests for messages, levels, structured properties, and exceptions.
| File | Description |
|---|---|
dotnet/tests/Microsoft.Agents.AI.Foundry.Hosting.UnitTests/SourceGeneratedLoggingTests.cs |
Adds focused logging-parity tests. |
dotnet/tests/Microsoft.Agents.AI.Foundry.Hosting.UnitTests/RecordingLoggerFactory.cs |
Captures structured log properties. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/HostedStoredOutputHealthCheckLogMessages.cs |
Defines generated health-check logs. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/HostedStoredOutputHealthCheck.cs |
Migrates health-check logging calls. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/HostedProtocolCompatibilityFilterLogMessages.cs |
Defines the generated protocol-rejection log. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/HostedProtocolCompatibilityFilter.cs |
Migrates protocol-rejection logging. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryToolboxServiceLogMessages.cs |
Defines generated toolbox logs. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryToolboxService.cs |
Migrates toolbox lifecycle and approval logging. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryJsonCheckpointStoreLogMessages.cs |
Defines generated checkpoint-store logs. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryJsonCheckpointStore.cs |
Migrates checkpoint retry and pruning logging. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryAgentSessionStoreLogMessages.cs |
Defines the generated missing-session log. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/FoundryAgentSessionStore.cs |
Migrates session-deletion logging. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/AgentFrameworkResponseHandlerLogMessages.cs |
Defines generated response-handler logs. |
dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/AgentFrameworkResponseHandler.cs |
Migrates response execution and resolution logging. |
Review effort: Balanced
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 7bf5699c2a23
Model: gpt-5.6-sol
Overview
The PR consistently migrates 29 Foundry Hosting log calls into component-local source-generated declarations, and focused tests preserve levels, templates, structured fields, and exception association. Existing enabled-level guards and privacy-sensitive arguments remain intact. However, the generated declarations do not preserve the prior event identity, which can break consumers that filter or query these logs by EventId.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/AgentFrameworkResponseHandlerLogMessages.cs
Code coverage overviewLanguages: C# C# / code-coverage/dotnet
View coverage of the 5 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
Source generation always assigns the method name as EventId.Name, but explicit EventId=0 preserves the numeric identity used by the direct Log* calls. Tests capture both fields to document the remaining name change.

Motivation & Context
The Foundry Hosting package still used direct
ILogger.Log*extension calls while the rest of the repository favors compile-time generated logging. Migrating these calls removes repeated runtime template processing and aligns the package with the established logging pattern without changing emitted log behavior.Description & Review Guide
Microsoft.Agents.AI.Foundry.Hostingto private[LoggerMessage]methods at the end of six partial owning types, matching the repository's dominant pattern for component-local generated logging. Removed manualIsEnabledchecks where generated methods provide the same guard. Enhanced the recording logger and existing component tests to assert levels, event metadata, templates, structured properties, and exception association.EventIdvalue of0. The source generator always assigns the generated method name toEventId.Name, including whenEventNameis explicitlynullor empty, so the name changes fromnullto the generated method name; tests capture both fields and this metadata difference is intentional for the source-generation migration. No OAuth consent URL, isolation value, credential, or token is added to logging. Validation passed:dotnet build src\Microsoft.Agents.AI.Foundry.Hosting\Microsoft.Agents.AI.Foundry.Hosting.csproj -c Debug --tl:off --warnaserror --no-restore— passed fornet8.0,net9.0, andnet10.0with 0 warnings and 0 errors.dotnet build tests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests.csproj -c Debug --tl:off --warnaserror --no-restore— passed fornet8.0,net9.0, andnet10.0with 0 warnings and 0 errors.dotnet test --project tests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests.csproj -f net10.0 -c Debug --no-build— passed: 644 succeeded, 0 failed, 1 pre-existing skipped test.dotnet format src\Microsoft.Agents.AI.Foundry.Hosting\Microsoft.Agents.AI.Foundry.Hosting.csproj --verify-no-changes --verbosity diagnostic— passed natively on Windows with 0 files changed.dotnet format tests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests\Microsoft.Agents.AI.Foundry.Hosting.UnitTests.csproj --verify-no-changes --verbosity diagnostic— passed natively on Windows with 0 files changed.EventId = 0, template/property casing, exception placement, and privacy-sensitive toolbox paths. The generated methods are intentionally colocated with their single owning component and do not use#region, matching nearby repository examples.Related Issue
N/A - trivial change. This is a mechanical internal logging refactor with no public behavior change, and no competing open pull request was found.
AI Assistance
AI assistance details: Copilot assisted with the logging migration, focused tests, convention review, EventId compatibility fix, validation, and this pull request description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and the title prefix in sync automatically.