Skip to content

Stream apphost logs across backchannel. #9990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 24, 2025
Merged
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
34 changes: 34 additions & 0 deletions src/Aspire.Cli/Backchannel/AppHostBackchannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal interface IAppHostBackchannel
Task<long> PingAsync(long timestamp, CancellationToken cancellationToken);
Task RequestStopAsync(CancellationToken cancellationToken);
Task<(string BaseUrlWithLoginToken, string? CodespacesUrlWithLoginToken)> GetDashboardUrlsAsync(CancellationToken cancellationToken);
IAsyncEnumerable<BackchannelLogEntry> GetAppHostLogEntriesAsync(CancellationToken cancellationToken);
IAsyncEnumerable<RpcResourceState> GetResourceStatesAsync(CancellationToken cancellationToken);
Task ConnectAsync(string socketPath, CancellationToken cancellationToken);
IAsyncEnumerable<PublishingActivity> GetPublishingActivitiesAsync(CancellationToken cancellationToken);
Expand Down Expand Up @@ -81,6 +82,27 @@ await rpc.InvokeWithCancellationAsync(
return (url.BaseUrlWithLoginToken, url.CodespacesUrlWithLoginToken);
}

public async IAsyncEnumerable<BackchannelLogEntry> GetAppHostLogEntriesAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
using var activity = telemetry.ActivitySource.StartActivity();

var rpc = await _rpcTaskCompletionSource.Task;

logger.LogDebug("Requesting AppHost log entries");

var logEntries = await rpc.InvokeWithCancellationAsync<IAsyncEnumerable<BackchannelLogEntry>>(
"GetAppHostLogEntriesAsync",
[],
cancellationToken);

logger.LogDebug("Received AppHost log entries async enumerable");

await foreach (var entry in logEntries.WithCancellation(cancellationToken))
{
yield return entry;
}
}

public async IAsyncEnumerable<RpcResourceState> GetResourceStatesAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
using var activity = telemetry.ActivitySource.StartActivity();
Expand Down Expand Up @@ -195,11 +217,23 @@ public async Task<string[]> GetCapabilitiesAsync(CancellationToken cancellationT
}
}

internal class BackchannelLogEntry
{
public required EventId EventId { get; set; }
public required LogLevel LogLevel { get; set; }
public required string Message { get; set; }
public Exception? Exception { get; set; }
public required DateTimeOffset Timestamp { get; set; }
public required string CategoryName { get; set; }
}

[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(DashboardUrlsState))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(IAsyncEnumerable<RpcResourceState>))]
[JsonSerializable(typeof(MessageFormatterEnumerableTracker.EnumeratorResults<RpcResourceState>))]
[JsonSerializable(typeof(IAsyncEnumerable<BackchannelLogEntry>))]
[JsonSerializable(typeof(MessageFormatterEnumerableTracker.EnumeratorResults<BackchannelLogEntry>))]
[JsonSerializable(typeof(IAsyncEnumerable<PublishingActivity>))]
[JsonSerializable(typeof(MessageFormatterEnumerableTracker.EnumeratorResults<PublishingActivity>))]
[JsonSerializable(typeof(RequestId))]
Expand Down
282 changes: 131 additions & 151 deletions src/Aspire.Cli/Commands/RunCommand.cs

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/Aspire.Hosting/Backchannel/AppHostRpcTarget.cs
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.Runtime.CompilerServices;
using System.Threading.Channels;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Dashboard;
using Aspire.Hosting.Devcontainers.Codespaces;
Expand All @@ -23,6 +24,32 @@ internal class AppHostRpcTarget(
DistributedApplicationOptions options
)
{
private readonly TaskCompletionSource<Channel<BackchannelLogEntry>> _logChannelTcs = new();

public void RegisterLogChannel(Channel<BackchannelLogEntry> channel)
{
ArgumentNullException.ThrowIfNull(channel);
_logChannelTcs.TrySetResult(channel);
}

public async IAsyncEnumerable<BackchannelLogEntry> GetAppHostLogEntriesAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
var channel = await _logChannelTcs.Task.WaitAsync(cancellationToken).ConfigureAwait(false);

var logEntries = channel.Reader.ReadAllAsync(cancellationToken);

await foreach (var logEntry in logEntries.WithCancellation(cancellationToken))
{
// If the log entry is null, terminate the stream
if (logEntry == null)
{
yield break;
}

yield return logEntry;
}
}

public async IAsyncEnumerable<PublishingActivity> GetPublishingActivitiesAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
while (cancellationToken.IsCancellationRequested == false)
Expand Down
101 changes: 101 additions & 0 deletions src/Aspire.Hosting/Backchannel/BackchannelLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Channels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Aspire.Hosting.Backchannel;

internal class BackchannelLoggerProvider : ILoggerProvider
{
private readonly Channel<BackchannelLogEntry> _channel = Channel.CreateUnbounded<BackchannelLogEntry>();
private readonly IServiceProvider _serviceProvider;
private readonly object _channelRegisteredLock = new();
private readonly CancellationTokenSource _backgroundChannelRegistrationCts = new();
private Task? _backgroundChannelRegistrationTask;

public BackchannelLoggerProvider(IServiceProvider serviceProvider)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
_serviceProvider = serviceProvider;
}

private void RegisterLogChannel()
{
// Why do we execute this on a background task? This method is spawned on a background
// task by the CreateLogger method. The CreateLogger method is called when creating many
// of the services registered in DI - but registering the log channel requires that we
// can resolve the AppHostRpcTarget service (thus creating a circular dependency). To resolve
// this we take a dependency on IServiceProvider so that on a separate background task we
// can resolve AppHostRpcTarget which in turn would have taken a dependency on a logger
// from this provider.
var target = _serviceProvider.GetRequiredService<AppHostRpcTarget>();
target.RegisterLogChannel(_channel);
}

public ILogger CreateLogger(string categoryName)
{
if (_backgroundChannelRegistrationTask == null)
{
lock (_channelRegisteredLock)
{
if (_backgroundChannelRegistrationTask == null)
{
_backgroundChannelRegistrationTask = Task.Run(
RegisterLogChannel,
_backgroundChannelRegistrationCts.Token);
}
}
}

return new BackchannelLogger(categoryName, _channel);
}

public void Dispose()
{
_backgroundChannelRegistrationCts.Cancel();
_channel.Writer.Complete();
}
}

internal class BackchannelLogger(string categoryName, Channel<BackchannelLogEntry> channel) : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return default;
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (IsEnabled(logLevel))
{
var entry = new BackchannelLogEntry
{
Timestamp = DateTimeOffset.UtcNow,
CategoryName = categoryName,
LogLevel = logLevel,
EventId = eventId,
Message = formatter(state, exception),
Exception = exception,
};

channel.Writer.TryWrite(entry);
}
}
}

internal class BackchannelLogEntry
{
public required EventId EventId { get; set; }
public required LogLevel LogLevel { get; set; }
public required string Message { get; set; }
public Exception? Exception { get; set; }
public required DateTimeOffset Timestamp { get; set; }
public required string CategoryName { get; set; }
}
2 changes: 0 additions & 2 deletions src/Aspire.Hosting/Backchannel/BackchannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ await eventing.PublishAsync(
backchannelConnectedEvent,
EventDispatchBehavior.NonBlockingConcurrent,
stoppingToken).ConfigureAwait(false);

logger.LogDebug("Accepted backchannel connection from {RemoteEndPoint}", clientSocket.RemoteEndPoint);
}
catch (TaskCanceledException ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Hosting/DistributedApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options)

_innerBuilder.Services.AddSingleton(TimeProvider.System);

_innerBuilder.Services.AddSingleton<ILoggerProvider, BackchannelLoggerProvider>();
_innerBuilder.Logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Warning);
_innerBuilder.Logging.AddFilter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Error);
_innerBuilder.Logging.AddFilter("Aspire.Hosting.Dashboard", LogLevel.Error);
Expand Down
39 changes: 28 additions & 11 deletions tests/Aspire.Cli.Tests/Commands/RunCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using Aspire.Cli.Backchannel;
using Aspire.Cli.Commands;
using Aspire.Cli.Projects;
using Aspire.Cli.Tests.TestServices;
using Aspire.Cli.Tests.Utils;
using Aspire.Cli.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;

namespace Aspire.Cli.Tests.Commands;
Expand Down Expand Up @@ -142,18 +144,38 @@ private sealed class MultipleProjectFilesProjectLocator : IProjectLocator
throw new Aspire.Cli.Projects.ProjectLocatorException("Multiple project files found.");
}
}
private async IAsyncEnumerable<BackchannelLogEntry> ReturnLogEntriesUntilCancelledAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
var logEntryIndex = 0;

while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(1000, cancellationToken);
// Simulate log entries being returned
yield return new BackchannelLogEntry
{
Timestamp = DateTimeOffset.UtcNow,
LogLevel = LogLevel.Information,
Message = $"Test log entry {logEntryIndex++}",
EventId = new EventId(),
CategoryName = "TestCategory"
};
}
}

[Fact]
public async Task RunCommand_CompletesSuccessfully()
{
var getResourceStatesAsyncCalled = new TaskCompletionSource();

var backchannelFactory = (IServiceProvider sp) => {
var backchannelFactory = (IServiceProvider sp) =>
{
var backchannel = new TestAppHostBackchannel();

backchannel.GetResourceStatesAsyncCalled = getResourceStatesAsyncCalled;
backchannel.GetAppHostLogEntriesAsyncCallback = ReturnLogEntriesUntilCancelledAsync;

return backchannel;

};

var runnerFactory = (IServiceProvider sp) => {
Expand Down Expand Up @@ -201,8 +223,6 @@ public async Task RunCommand_CompletesSuccessfully()
using var cts = new CancellationTokenSource();
var pendingRun = result.InvokeAsync(cts.Token);

await getResourceStatesAsyncCalled.Task.WaitAsync(CliTestConstants.DefaultTimeout);

// Simulate CTRL-C.
cts.Cancel();

Expand All @@ -216,11 +236,10 @@ public async Task RunCommand_WithNoResources_CompletesSuccessfully()
var getResourceStatesAsyncCalled = new TaskCompletionSource();
var backchannelFactory = (IServiceProvider sp) => {
var backchannel = new TestAppHostBackchannel();
backchannel.GetResourceStatesAsyncCalled = getResourceStatesAsyncCalled;


// Return empty resources using an empty enumerable
backchannel.GetResourceStatesAsyncCallback = _ => EmptyAsyncEnumerable<RpcResourceState>.Instance;
backchannel.GetAppHostLogEntriesAsyncCallback = ReturnLogEntriesUntilCancelledAsync;

return backchannel;
};

Expand Down Expand Up @@ -258,12 +277,10 @@ public async Task RunCommand_WithNoResources_CompletesSuccessfully()
using var cts = new CancellationTokenSource();
var pendingRun = result.InvokeAsync(cts.Token);

await getResourceStatesAsyncCalled.Task.WaitAsync(CliTestConstants.DefaultTimeout);

// Simulate CTRL-C.
cts.Cancel();

var exitCode = await pendingRun.WaitAsync(CliTestConstants.DefaultTimeout);
var exitCode = await pendingRun.WaitAsync(CliTestConstants.LongTimeout);
Assert.Equal(ExitCodeConstants.Success, exitCode);
}
}
15 changes: 15 additions & 0 deletions tests/Aspire.Cli.Tests/TestServices/TestAppHostBackchannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ internal sealed class TestAppHostBackchannel : IAppHostBackchannel
public TaskCompletionSource? GetResourceStatesAsyncCalled { get; set; }
public Func<CancellationToken, IAsyncEnumerable<RpcResourceState>>? GetResourceStatesAsyncCallback { get; set; }

public TaskCompletionSource? GetAppHostLogEntriesAsyncCalled { get; set; }
public Func<CancellationToken, IAsyncEnumerable<BackchannelLogEntry>>? GetAppHostLogEntriesAsyncCallback { get; set; }

public TaskCompletionSource? ConnectAsyncCalled { get; set; }
public Func<string, CancellationToken, Task>? ConnectAsyncCallback { get; set; }

Expand Down Expand Up @@ -237,4 +240,16 @@ public async Task<string[]> GetCapabilitiesAsync(CancellationToken cancellationT
return ["baseline.v2"];
}
}

public async IAsyncEnumerable<BackchannelLogEntry> GetAppHostLogEntriesAsync([EnumeratorCancellation]CancellationToken cancellationToken)
{
GetAppHostLogEntriesAsyncCalled?.SetResult();
if (GetAppHostLogEntriesAsyncCallback != null)
{
await foreach (var entry in GetAppHostLogEntriesAsyncCallback.Invoke(cancellationToken))
{
yield return entry;
}
}
}
}
Loading