Skip to content

Add Logger property to ExecuteCommandContext - #15678

Merged
James Newton-King (JamesNK) merged 4 commits into
microsoft:mainfrom
tranhoangtu-it:add-logger-to-execute-command-context
Mar 31, 2026
Merged

James Newton-King (JamesNK) merged 4 commits into
microsoft:mainfrom
tranhoangtu-it:add-logger-to-execute-command-context

Conversation

@tranhoangtu-it

Copy link
Copy Markdown
Contributor

Fixes #15582

Adds a Logger property to ExecuteCommandContext so command handlers can log directly to the resource's log stream without resolving it from ServiceProvider.

The logger instance comes from ResourceLoggerService.GetLogger(resourceId), which is already obtained in ResourceCommandService — this change just passes it through to the context.

Changes

  • ResourceCommandAnnotation.cs — Added required ILogger Logger { get; init; } to ExecuteCommandContext
  • ResourceCommandService.cs — Passes existing logger when constructing the context
  • Aspire.Hosting.cs — Updated API surface file

@github-actions

github-actions Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15678

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15678"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a Logger property to ExecuteCommandContext so resource command handlers can log directly to the resource’s log stream without resolving a logger from IServiceProvider.

Changes:

  • Added ILogger Logger to ExecuteCommandContext (public API).
  • Passed the existing resource logger into ExecuteCommandContext from ResourceCommandService.
  • Updated the public API surface file to include the new property.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Aspire.Hosting/ApplicationModel/ResourceCommandAnnotation.cs Extends ExecuteCommandContext with a Logger property.
src/Aspire.Hosting/ApplicationModel/ResourceCommandService.cs Populates ExecuteCommandContext.Logger using the existing ResourceLoggerService logger.
src/Aspire.Hosting/api/Aspire.Hosting.cs Updates API surface to include the new Logger property.

Comment on lines 245 to +250
public required CancellationToken CancellationToken { get; init; }

/// <summary>
/// The logger for the resource.
/// </summary>
public required ILogger Logger { get; init; }

Copilot AI Mar 28, 2026

Copy link

Choose a reason for hiding this comment

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

Adding required ILogger Logger makes ExecuteCommandContext source-breaking for any consumers that construct it (e.g., tests) and is inconsistent with other callback contexts that default Logger to NullLogger.Instance. Consider making Logger non-required and initializing it to NullLogger.Instance (and still setting it in ResourceCommandService) so the property is always non-null without forcing callers to provide it.

Copilot uses AI. Check for mistakes.

@JamesNK James Newton-King (JamesNK) Mar 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with this. It's only a source breaking change and I doubt many people create this type themselves.

Simple enough to fix by assigning a NullLogger.Instance in tests on upgrade to fix.

The alternative is not making it required and assigning it = null!;. Trades a build time error for runtime errors.

Comment on lines 245 to +250
public required CancellationToken CancellationToken { get; init; }

/// <summary>
/// The logger for the resource.
/// </summary>
public required ILogger Logger { get; init; }

@JamesNK James Newton-King (JamesNK) Mar 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with this. It's only a source breaking change and I doubt many people create this type themselves.

Simple enough to fix by assigning a NullLogger.Instance in tests on upgrade to fix.

The alternative is not making it required and assigning it = null!;. Trades a build time error for runtime errors.

@JamesNK

Copy link
Copy Markdown
Member

Sébastien Ros (@sebastienros) What's required when adding new API to exported type? Is something special required for ILogger?

@sebastienros

Copy link
Copy Markdown
Contributor

James Newton-King (@JamesNK) It's not breaking, and ILogger/ILoggerFactory is supported by ATS. (same for IServiceProvider, and CancellationToken btw).

@sebastienros

Copy link
Copy Markdown
Contributor

Here for the build to pass the snapshots need to be regenerated (running the tests locally for instance will do it). This change is surely updating the generated SDKs.

Added Logger getter/setter methods to all 5 language SDK snapshots
(Go, Java, Python, Rust, TypeScript) following the existing pattern
from EnvironmentCallbackContext.Logger.
@tranhoangtu-it
Trần Hoàng Tú (tranhoangtu-it) force-pushed the add-logger-to-execute-command-context branch from 570c5fb to bbecc63 Compare March 30, 2026 17:29
@tranhoangtu-it

Copy link
Copy Markdown
Contributor Author

Updated! Reverted the manual API surface file edit (auto-generated per David Fowler (@davidfowl)) and regenerated all 5 language SDK snapshots for the new Logger property. The snapshots follow the existing pattern from EnvironmentCallbackContext.Logger.

@JamesNK
James Newton-King (JamesNK) merged commit 309c71e into microsoft:main Mar 31, 2026
256 checks passed
@joperezr Jose Perez Rodriguez (joperezr) added this to the 13.3 milestone Apr 14, 2026
@aspire-repo-bot

Copy link
Copy Markdown
Contributor

📝 Documentation updates have been drafted for this PR.

A draft PR has been opened on microsoft/aspire.dev targeting release/13.3 (matching the PR milestone).

Summary of documentation changes in fundamentals/custom-resource-commands.mdx:

  • Added ExecuteCommandContext.Logger to the properties list, explaining it provides an ILogger scoped to the resource's log stream
  • Updated the "Execute commands from a custom command" code example to use context.Logger directly instead of resolving ILogger<Program> from the service provider
  • Updated surrounding descriptive text to highlight the benefit of the new property

The draft PR needs human review before merging.

Generated by PR Documentation Check · ● 845.8K · ◷

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Logger property to ExecuteCommandContext

6 participants