Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Aspire.Hosting.ApplicationModel;

Expand Down Expand Up @@ -242,4 +243,9 @@ public sealed class ExecuteCommandContext
/// The cancellation token.
/// </summary>
public required CancellationToken CancellationToken { get; init; }

/// <summary>
/// The logger for the resource.
/// </summary>
public required ILogger Logger { get; init; }
Comment on lines 245 to +250
Copy link

Copilot AI Mar 28, 2026

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.
Copy link
Copy Markdown
Member

@JamesNK JamesNK Mar 30, 2026

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.

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ internal async Task<ExecuteCommandResult> ExecuteCommandCoreAsync(string resourc
{
ResourceName = resourceId,
ServiceProvider = _serviceProvider,
CancellationToken = cancellationToken
CancellationToken = cancellationToken,
Logger = logger
};

var result = await annotation.ExecuteCommand(context).ConfigureAwait(false);
Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting/api/Aspire.Hosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,8 @@ public sealed partial class ExecuteCommandContext
{
public required System.Threading.CancellationToken CancellationToken { get { throw null; } init { } }

public required Microsoft.Extensions.Logging.ILogger Logger { get { throw null; } init { } }

public required string ResourceName { get { throw null; } init { } }

public required System.IServiceProvider ServiceProvider { get { throw null; } init { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8187,6 +8187,31 @@ func (s *ExecuteCommandContext) SetCancellationToken(value *CancellationToken) (
return result.(*ExecuteCommandContext), nil
}

// Logger gets the Logger property
func (s *ExecuteCommandContext) Logger() (*ILogger, error) {
reqArgs := map[string]any{
"context": SerializeValue(s.Handle()),
}
result, err := s.Client().InvokeCapability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.logger", reqArgs)
if err != nil {
return nil, err
}
return result.(*ILogger), nil
}

// SetLogger sets the Logger property
func (s *ExecuteCommandContext) SetLogger(value *ILogger) (*ExecuteCommandContext, error) {
reqArgs := map[string]any{
"context": SerializeValue(s.Handle()),
}
reqArgs["value"] = SerializeValue(value)
result, err := s.Client().InvokeCapability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.setLogger", reqArgs)
if err != nil {
return nil, err
}
return result.(*ExecuteCommandContext), nil
}

// ExternalServiceResource wraps a handle for Aspire.Hosting/Aspire.Hosting.ExternalServiceResource.
type ExternalServiceResource struct {
ResourceBuilderBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9131,6 +9131,25 @@ public ExecuteCommandContext setCancellationToken(CancellationToken value) {
return (ExecuteCommandContext) getClient().invokeCapability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.setCancellationToken", reqArgs);
}

/** Gets the Logger property */
public ILogger logger() {
Map<String, Object> reqArgs = new HashMap<>();
reqArgs.put("context", AspireClient.serializeValue(getHandle()));
return (ILogger) getClient().invokeCapability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.logger", reqArgs);
}

/** Sets the Logger property */
public ExecuteCommandContext setLogger(ILogger value) {
Map<String, Object> reqArgs = new HashMap<>();
reqArgs.put("context", AspireClient.serializeValue(getHandle()));
reqArgs.put("value", AspireClient.serializeValue(value));
return (ExecuteCommandContext) getClient().invokeCapability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.setLogger", reqArgs);
}

public ExecuteCommandContext setLogger(HandleWrapperBase value) {
return setLogger(new ILogger(value.getHandle(), value.getClient()));
}

}

// ===== ExecuteCommandResult.java =====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3343,6 +3343,23 @@ def cancel(self) -> None:
)
token.cancel()

@_uncached_property
def logger(self) -> AbstractLogger:
"""Gets the Logger property"""
result = self._client.invoke_capability(
'Aspire.Hosting.ApplicationModel/ExecuteCommandContext.logger',
{'context': self._handle}
)
return typing.cast(AbstractLogger, result)

@logger.setter
def logger(self, value: AbstractLogger) -> None:
"""Sets the Logger property"""
self._client.invoke_capability(
'Aspire.Hosting.ApplicationModel/ExecuteCommandContext.setLogger',
{'context': self._handle, 'value': value}
)


class InitializeResourceEvent:
"""Type class for InitializeResourceEvent."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7116,6 +7116,25 @@ impl ExecuteCommandContext {
let handle: Handle = serde_json::from_value(result)?;
Ok(ExecuteCommandContext::new(handle, self.client.clone()))
}

/// Gets the Logger property
pub fn logger(&self) -> Result<ILogger, Box<dyn std::error::Error>> {
let mut args: HashMap<String, Value> = HashMap::new();
args.insert("context".to_string(), self.handle.to_json());
let result = self.client.invoke_capability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.logger", args)?;
let handle: Handle = serde_json::from_value(result)?;
Ok(ILogger::new(handle, self.client.clone()))
}

/// Sets the Logger property
pub fn set_logger(&self, value: &ILogger) -> Result<ExecuteCommandContext, Box<dyn std::error::Error>> {
let mut args: HashMap<String, Value> = HashMap::new();
args.insert("context".to_string(), self.handle.to_json());
args.insert("value".to_string(), value.handle().to_json());
let result = self.client.invoke_capability("Aspire.Hosting.ApplicationModel/ExecuteCommandContext.setLogger", args)?;
let handle: Handle = serde_json::from_value(result)?;
Ok(ExecuteCommandContext::new(handle, self.client.clone()))
}
}

/// Wrapper for Aspire.Hosting/Aspire.Hosting.ExternalServiceResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,17 @@ export class ExecuteCommandContext {
}
};

/** Gets the Logger property */
logger = {
get: async (): Promise<Logger> => {
const handle = await this._client.invokeCapability<ILoggerHandle>(
'Aspire.Hosting.ApplicationModel/ExecuteCommandContext.logger',
{ context: this._handle }
);
return new Logger(handle, this._client);
},
};

}

// ============================================================================
Expand Down
Loading