Skip to content

Commit

Permalink
Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Sep 14, 2024
1 parent 02488ce commit fcd29a0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,19 @@ private async ValueTask LoadLogsAsync()
}
else
{
Logger.LogDebug("Subscribing to console logs for resource {ResourceName}.", PageViewModel.SelectedResource.Name);
var resourceSubscriptionName = PageViewModel.SelectedResource.Name;

Logger.LogDebug("Subscribing to console logs for resource {ResourceName}.", resourceSubscriptionName);

var cancellationToken = await _logSubscriptionCancellationSeries.NextAsync();

var subscription = DashboardClient.SubscribeConsoleLogs(PageViewModel.SelectedResource.Name, cancellationToken);
var subscription = DashboardClient.SubscribeConsoleLogs(resourceSubscriptionName, cancellationToken);

if (subscription is not null)
{
var task = _logViewer.SetLogSourceAsync(PageViewModel.SelectedResource.Name, subscription);
var task = _logViewer.SetLogSourceAsync(resourceSubscriptionName, subscription);

_subscriptionResourceName = PageViewModel.SelectedResource.Name;
_subscriptionResourceName = resourceSubscriptionName;
PageViewModel.Status = Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsWatchingLogs)];

_ = Task.Run(async () =>
Expand All @@ -292,6 +294,8 @@ private async ValueTask LoadLogsAsync()
}
finally
{
Logger.LogDebug("Finished watching logs for resource {ResourceName}.", resourceSubscriptionName);
_subscriptionResourceName = null;
PageViewModel.Status = Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsFinishedWatchingLogs)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<Compile Include="$(TestsSharedDir)Telemetry\*.cs" LinkBase="shared/Telemetry" />
<Compile Include="$(TestsSharedDir)Logging\*.cs" LinkBase="shared/Logging" />
</ItemGroup>

</Project>
20 changes: 16 additions & 4 deletions tests/Aspire.Dashboard.Components.Tests/Pages/ConsoleLogsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.FluentUI.AspNetCore.Components;
using Xunit;
using Xunit.Abstractions;

namespace Aspire.Dashboard.Components.Tests.Pages;

[UseCulture("en-US")]
public partial class ConsoleLogsTests : TestContext
{
private readonly ITestOutputHelper _testOutputHelper;

public ConsoleLogsTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void ResourceName_MultiRender_SubscribeConsoleLogsOnce()
{
Expand Down Expand Up @@ -51,24 +60,24 @@ public void ResourceName_MultiRender_SubscribeConsoleLogsOnce()
});

var instance = cut.Instance;
var logger = Services.GetRequiredService<ILogger<ConsoleLogsTests>>();

var testResource = CreateResourceViewModel("test-resource", KnownResourceState.Running);
resourceChannel.Writer.TryWrite([
new ResourceViewModelChange(ResourceViewModelChangeType.Upsert, testResource)
]);

// Assert
logger.LogInformation("Waiting for selected resource.");
cut.WaitForState(() => instance.PageViewModel.SelectedResource == testResource);

viewport = new ViewportInformation(IsDesktop: false, IsUltraLowHeight: false, IsUltraLowWidth: false);
dimensionManager.InvokeOnViewportInformationChanged(viewport);

// Re-invoke
// Ensure component is rendered again.
cut.SetParametersAndRender(builder => { });

consoleLogsChannel.Writer.TryWrite([new ResourceLogLine(1, "Test content", IsErrorMessage: false)]);
consoleLogsChannel.Writer.Complete();

logger.LogInformation("Waiting for finish message.");
var loc = Services.GetRequiredService<IStringLocalizer<Resources.ConsoleLogs>>();
cut.WaitForState(() => instance.PageViewModel.Status == loc[nameof(Resources.ConsoleLogs.ConsoleLogsFinishedWatchingLogs)]);

Expand Down Expand Up @@ -96,7 +105,10 @@ private void SetupConsoleLogsServices(TestDashboardClient? dashboardClient = nul
JSInterop.SetupVoid("initializeContinuousScroll");
JSInterop.SetupVoid("resetContinuousScrollPosition");

var loggerFactory = IntegrationTestHelpers.CreateLoggerFactory(_testOutputHelper);

Services.AddLocalization();
Services.AddSingleton<ILoggerFactory>(loggerFactory);
Services.AddSingleton<BrowserTimeProvider, TestTimeProvider>();
Services.AddSingleton<IMessageService, MessageService>();
Services.AddSingleton<IOptions<DashboardOptions>>(Options.Create(new DashboardOptions()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit.Abstractions;

namespace Aspire.Dashboard.Components.Tests.Shared;

public static class IntegrationTestHelpers
{
public static ILoggerFactory CreateLoggerFactory(ITestOutputHelper testOutputHelper, ITestSink? testSink = null)
{
return LoggerFactory.Create(builder =>
{
builder.AddXunit(testOutputHelper, LogLevel.Trace, DateTimeOffset.UtcNow);
builder.SetMinimumLevel(LogLevel.Trace);
if (testSink != null)
{
builder.AddProvider(new TestLoggerProvider(testSink));
}
});
}
}

0 comments on commit fcd29a0

Please sign in to comment.