|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading.Channels; |
| 7 | +using Aspire.Hosting.Dashboard; |
| 8 | +using Aspire.Hosting.Dcp; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.Hosting; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using Microsoft.Extensions.Logging.Testing; |
| 14 | +using Microsoft.Extensions.Options; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Aspire.Hosting.Tests.Dashboard; |
| 18 | + |
| 19 | +public class DashboardLifecycleHookTests |
| 20 | +{ |
| 21 | + [Theory] |
| 22 | + [MemberData(nameof(Data))] |
| 23 | + public async Task WatchDashboardLogs_WrittenToHostLoggerFactory(string logMessage, string expectedMessage, string expectedCategory, LogLevel expectedLevel) |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var testSink = new TestSink(); |
| 27 | + var factory = LoggerFactory.Create(b => |
| 28 | + { |
| 29 | + b.SetMinimumLevel(LogLevel.Trace); |
| 30 | + b.AddProvider(new TestLoggerProvider(testSink)); |
| 31 | + }); |
| 32 | + var logChannel = Channel.CreateUnbounded<WriteContext>(); |
| 33 | + testSink.MessageLogged += c => logChannel.Writer.TryWrite(c); |
| 34 | + |
| 35 | + var resourceLoggerService = new ResourceLoggerService(); |
| 36 | + var resourceNotificationService = new ResourceNotificationService(NullLogger<ResourceNotificationService>.Instance, new TestHostApplicationLifetime()); |
| 37 | + var configuration = new ConfigurationBuilder().Build(); |
| 38 | + var hook = new DashboardLifecycleHook( |
| 39 | + configuration, |
| 40 | + Options.Create(new DashboardOptions { DashboardPath = "test.dll" }), |
| 41 | + NullLogger<DistributedApplication>.Instance, |
| 42 | + new TestDashboardEndpointProvider(), |
| 43 | + new DistributedApplicationExecutionContext(DistributedApplicationOperation.Run), |
| 44 | + resourceNotificationService, |
| 45 | + resourceLoggerService, |
| 46 | + factory); |
| 47 | + |
| 48 | + var model = new DistributedApplicationModel(new ResourceCollection()); |
| 49 | + await hook.BeforeStartAsync(model, CancellationToken.None); |
| 50 | + |
| 51 | + await resourceNotificationService.PublishUpdateAsync(model.Resources.Single(), s => s); |
| 52 | + |
| 53 | + await foreach (var item in resourceLoggerService.WatchAnySubscribersAsync()) |
| 54 | + { |
| 55 | + if (item.Name == KnownResourceNames.AspireDashboard && item.AnySubscribers) |
| 56 | + { |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Act |
| 62 | + var dashboardLogger = resourceLoggerService.GetLogger(KnownResourceNames.AspireDashboard); |
| 63 | + dashboardLogger.LogError(logMessage); |
| 64 | + |
| 65 | + // Assert |
| 66 | + var logContext = await logChannel.Reader.ReadAsync(); |
| 67 | + Assert.Equal(expectedCategory, logContext.LoggerName); |
| 68 | + Assert.Equal(expectedMessage, logContext.Message); |
| 69 | + Assert.Equal(expectedLevel, logContext.LogLevel); |
| 70 | + } |
| 71 | + |
| 72 | + public static IEnumerable<object[]> Data() |
| 73 | + { |
| 74 | + var timestamp = new DateTime(2001, 12, 29, 23, 59, 59, DateTimeKind.Utc); |
| 75 | + var message = new DashboardLogMessage |
| 76 | + { |
| 77 | + LogLevel = LogLevel.Error, |
| 78 | + Category = "TestCategory", |
| 79 | + Message = "Hello world", |
| 80 | + Timestamp = timestamp.ToString(KnownFormats.ConsoleLogsTimestampFormat, CultureInfo.InvariantCulture), |
| 81 | + }; |
| 82 | + var messageJson = JsonSerializer.Serialize(message, DashboardLogMessageContext.Default.DashboardLogMessage); |
| 83 | + |
| 84 | + yield return new object[] |
| 85 | + { |
| 86 | + $"{DateTime.UtcNow.ToString(KnownFormats.ConsoleLogsTimestampFormat, CultureInfo.InvariantCulture)} {messageJson}", |
| 87 | + "Hello world", |
| 88 | + "Aspire.Hosting.Dashboard.TestCategory", |
| 89 | + LogLevel.Error |
| 90 | + }; |
| 91 | + yield return new object[] |
| 92 | + { |
| 93 | + $"{DateTime.UtcNow.ToString(KnownFormats.ConsoleLogsTimestampFormat, CultureInfo.InvariantCulture)}{messageJson}", |
| 94 | + "Hello world", |
| 95 | + "Aspire.Hosting.Dashboard.TestCategory", |
| 96 | + LogLevel.Error |
| 97 | + }; |
| 98 | + yield return new object[] |
| 99 | + { |
| 100 | + messageJson, |
| 101 | + "Hello world", |
| 102 | + "Aspire.Hosting.Dashboard.TestCategory", |
| 103 | + LogLevel.Error |
| 104 | + }; |
| 105 | + |
| 106 | + message = new DashboardLogMessage |
| 107 | + { |
| 108 | + LogLevel = LogLevel.Critical, |
| 109 | + Category = "TestCategory.TestSubCategory", |
| 110 | + Message = "Error message", |
| 111 | + Exception = new InvalidOperationException("Error!").ToString(), |
| 112 | + Timestamp = timestamp.ToString(KnownFormats.ConsoleLogsTimestampFormat, CultureInfo.InvariantCulture), |
| 113 | + }; |
| 114 | + messageJson = JsonSerializer.Serialize(message, DashboardLogMessageContext.Default.DashboardLogMessage); |
| 115 | + |
| 116 | + yield return new object[] |
| 117 | + { |
| 118 | + messageJson, |
| 119 | + $"Error message{Environment.NewLine}System.InvalidOperationException: Error!", |
| 120 | + "Aspire.Hosting.Dashboard.TestCategory.TestSubCategory", |
| 121 | + LogLevel.Critical |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + private sealed class TestDashboardEndpointProvider : IDashboardEndpointProvider |
| 126 | + { |
| 127 | + public Task<string> GetResourceServiceUriAsync(CancellationToken cancellationToken = default) |
| 128 | + { |
| 129 | + throw new NotImplementedException(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private sealed class TestHostApplicationLifetime : IHostApplicationLifetime |
| 134 | + { |
| 135 | + public CancellationToken ApplicationStarted { get; } |
| 136 | + public CancellationToken ApplicationStopped { get; } |
| 137 | + public CancellationToken ApplicationStopping { get; } |
| 138 | + |
| 139 | + public void StopApplication() |
| 140 | + { |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments