Skip to content
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

[tests] Add resource logging to EndToEnd tests #5328

Merged
merged 2 commits into from
Aug 17, 2024
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
54 changes: 34 additions & 20 deletions src/Aspire.Hosting.Testing/ResourceLoggerForwarderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,54 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)

private async Task WatchNotifications(CancellationToken cancellationToken)
{
var loggingResourceIds = new HashSet<string>();
var logWatchTasks = new List<Task>();

await foreach (var resourceEvent in resourceNotificationService.WatchAsync(cancellationToken).ConfigureAwait(false))
try
{
var resourceId = resourceEvent.ResourceId;
var loggingResourceIds = new HashSet<string>();
var logWatchTasks = new List<Task>();

if (loggingResourceIds.Add(resourceId))
await foreach (var resourceEvent in resourceNotificationService.WatchAsync(cancellationToken).ConfigureAwait(false))
{
// Start watching the logs for this resource ID
logWatchTasks.Add(WatchResourceLogs(resourceEvent.Resource, resourceId, cancellationToken));
var resourceId = resourceEvent.ResourceId;

if (loggingResourceIds.Add(resourceId))
{
// Start watching the logs for this resource ID
logWatchTasks.Add(WatchResourceLogs(resourceEvent.Resource, resourceId, cancellationToken));
}
}
}

await Task.WhenAll(logWatchTasks).ConfigureAwait(false);
await Task.WhenAll(logWatchTasks).ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
Copy link
Member

Choose a reason for hiding this comment

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

👍

{
// this was expected as the token was canceled
}
}

private async Task WatchResourceLogs(IResource resource, string resourceId, CancellationToken cancellationToken)
{
var applicationName = hostEnvironment.ApplicationName;
var logger = loggerFactory.CreateLogger($"{applicationName}.Resources.{resource.Name}");
await foreach (var logEvent in resourceLoggerService.WatchAsync(resourceId).WithCancellation(cancellationToken).ConfigureAwait(false))
try
{
foreach (var line in logEvent)
var applicationName = hostEnvironment.ApplicationName;
var logger = loggerFactory.CreateLogger($"{applicationName}.Resources.{resource.Name}");
await foreach (var logEvent in resourceLoggerService.WatchAsync(resourceId).WithCancellation(cancellationToken).ConfigureAwait(false))
{
var logLevel = line.IsErrorMessage ? LogLevel.Error : LogLevel.Information;

if (logger.IsEnabled(logLevel))
foreach (var line in logEvent)
{
// Log message format here approximates the format shown in the dashboard
logger.Log(logLevel, "{LineNumber}: {LineContent}", line.LineNumber, line.Content);
OnResourceLog?.Invoke(resourceId);
var logLevel = line.IsErrorMessage ? LogLevel.Error : LogLevel.Information;

if (logger.IsEnabled(logLevel))
{
// Log message format here approximates the format shown in the dashboard
logger.Log(logLevel, "{LineNumber}: {LineContent}", line.LineNumber, line.Content);
OnResourceLog?.Invoke(resourceId);
}
}
}
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// this was expected as the token was canceled
}
}
}
2 changes: 2 additions & 0 deletions tests/Aspire.EndToEnd.Tests/Aspire.EndToEnd.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

<ItemGroup Condition="'$(_BuildForTestsRunningOutsideOfRepo)' == 'true'">
<None Include="..\testproject\**\*" Link="$(DeployOutsideOfRepoSupportFilesDir)%(RecursiveDir)%(FileName)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
<!-- Needed by TestProject.AppHost -->
<None Include="$(RepoRoot)src\Aspire.Hosting.Testing\ResourceLoggerForwarderService.cs" Link="$(DeployOutsideOfRepoSupportFilesDir)TestProject.AppHost\ResourceLoggerForwarderService.cs" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\.editorconfig" Link="$(DeployOutsideOfRepoSupportFilesDir)..\%(FileName)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions tests/testproject/TestProject.AppHost/TestProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Aspire.Hosting.Lifecycle;
using Aspire.Hosting.Testing;
using Aspire.TestProject;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -101,6 +102,7 @@ private TestProgram(
}
}

AppBuilder.Services.AddHostedService<ResourceLoggerForwarderService>();
AppBuilder.Services.AddLifecycleHook<EndPointWriterHook>();
AppBuilder.Services.AddHttpClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<ItemGroup>
<Compile Include="..\Common\TestResourceNames.cs" />

<!-- on helix, the file will be in the source directory, so it will get
picked up by msbuild by default -->
<Compile Condition="'$(RepoRoot)' != ''" Include="$(RepoRoot)src\Aspire.Hosting.Testing\ResourceLoggerForwarderService.cs" Link="Utils\ResourceLoggerForwarderService.cs" />

<ProjectReference Include="..\TestProject.IntegrationServiceA\TestProject.IntegrationServiceA.csproj" AspireProjectMetadataTypeName="IntegrationServiceA" />
<ProjectReference Include="..\TestProject.ServiceA\TestProject.ServiceA.csproj" AspireProjectMetadataTypeName="ServiceA" />
<ProjectReference Include="..\TestProject.ServiceB\TestProject.ServiceB.csproj" AspireProjectMetadataTypeName="ServiceB" />
Expand Down