Skip to content
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
20 changes: 12 additions & 8 deletions src/Shared/LoggingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,41 @@ public static void WriteDashboardSummary(ILogger logger, string? dashboardUrl, s
.Append("Aspire Dashboard").Append('\n')
.Append('\n');

// The default .NET console logger indents the first line 6 characters because of the level.
// This prefix aligns the URLs under the dashboard title and makes them easier to read.
// In other logging layouts the prefix is just extra spaces, but it doesn't cause any real issues.
var prefix = " ";

if (dashboardAuthority is not null)
{
templateBuilder.Append("Dashboard: {DashboardUrl}").Append('\n');
templateBuilder.Append(prefix).Append("- Dashboard: {DashboardUrl}").Append('\n');
parameters.Add(dashboardAuthority);
}

if (loginUrl is not null)
{
templateBuilder.Append("Login URL: {LoginUrl}").Append('\n');
templateBuilder.Append(prefix).Append("- Login URL: {LoginUrl}").Append('\n');
parameters.Add(loginUrl);
}

if (otlpGrpcAuthority is not null)
{
templateBuilder.Append("OTLP/gRPC: {OtlpGrpcUrl}").Append('\n');
templateBuilder.Append(prefix).Append("- OTLP/gRPC: {OtlpGrpcUrl}").Append('\n');
parameters.Add(otlpGrpcAuthority);
}

if (otlpHttpAuthority is not null)
{
templateBuilder.Append("OTLP/HTTP: {OtlpHttpUrl}").Append('\n');
templateBuilder.Append(prefix).Append("- OTLP/HTTP: {OtlpHttpUrl}").Append('\n');
parameters.Add(otlpHttpAuthority);
}

logger.LogInformation(templateBuilder.ToString(), parameters.ToArray());

if (isContainer)
{
templateBuilder.Append('\n');
templateBuilder.Append("URLs may need changes depending on how network access to the container is configured.").Append('\n');
logger.LogInformation("Dashboard is running in a container. Access the dashboard from the host using port forwarding.");
}

logger.LogInformation(templateBuilder.ToString(), parameters.ToArray());
}

[Conditional("DEBUG")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ public async Task LogOutput_InContainer_LoginLinkContainerMessage()
// Assert
var l = testSink.Writes.Where(w => w.LoggerName == typeof(DashboardWebApplication).FullName).ToList();

// The container message is now part of the summary log message.
var summaryLog = l.Single(w => ((string?)LogTestHelpers.GetValue(w, "{OriginalFormat}"))?.StartsWith("Aspire Dashboard") == true);
var containerMessage = "URLs may need changes depending on how network access to the container is configured.";
Assert.Contains(containerMessage, summaryLog.Message);
// The container message is a separate log entry from the summary.
var containerLog = l.Single(w => w.Message == "Dashboard is running in a container. Access the dashboard from the host using port forwarding.");
Assert.NotNull(containerLog);
}
}
36 changes: 16 additions & 20 deletions tests/Aspire.Dashboard.Tests/LoggingHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public void WriteDashboardSummary_WithTokenAndOtlpEndpoints_LogsSummaryAndStruct
Assert.Collection(lines,
line => Assert.Equal("Aspire Dashboard", line),
line => Assert.Equal(string.Empty, line),
line => Assert.Equal("Dashboard: http://localhost:18888", line),
line => Assert.Equal("Login URL: http://localhost:18888/login?t=abc123", line),
line => Assert.Equal("OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal("OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(" - Dashboard: http://localhost:18888", line),
line => Assert.Equal(" - Login URL: http://localhost:18888/login?t=abc123", line),
line => Assert.Equal(" - OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal(" - OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(string.Empty, line));

Assert.Equal("http://localhost:18888", LogTestHelpers.GetValue(write, "DashboardUrl"));
Expand Down Expand Up @@ -72,9 +72,9 @@ public void WriteDashboardSummary_WithoutToken_DoesNotIncludeLoginUrl()
Assert.Collection(lines,
line => Assert.Equal("Aspire Dashboard", line),
line => Assert.Equal(string.Empty, line),
line => Assert.Equal("Dashboard: http://localhost:18888", line),
line => Assert.Equal("OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal("OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(" - Dashboard: http://localhost:18888", line),
line => Assert.Equal(" - OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal(" - OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(string.Empty, line));

Assert.Null(LogTestHelpers.GetValue(write, "LoginUrl"));
Expand All @@ -96,8 +96,8 @@ public void WriteDashboardSummary_InvalidDashboardUrl_LogsOtlpEndpoints()
Assert.Collection(lines,
line => Assert.Equal("Aspire Dashboard", line),
line => Assert.Equal(string.Empty, line),
line => Assert.Equal("OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal("OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(" - OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal(" - OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(string.Empty, line));
}

Expand All @@ -122,8 +122,8 @@ public void WriteDashboardSummary_NullDashboardUrl_LogsOtlpEndpoints()
Assert.Collection(lines,
line => Assert.Equal("Aspire Dashboard", line),
line => Assert.Equal(string.Empty, line),
line => Assert.Equal("OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal("OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(" - OTLP/gRPC: http://localhost:18889", line),
line => Assert.Equal(" - OTLP/HTTP: http://localhost:18890", line),
line => Assert.Equal(string.Empty, line));

Assert.Null(LogTestHelpers.GetValue(write, "DashboardUrl"));
Expand Down Expand Up @@ -186,8 +186,8 @@ public void WriteDashboardSummary_WithoutOtlpEndpoints_DoesNotIncludeOtlpLines()
Assert.Collection(lines,
line => Assert.Equal("Aspire Dashboard", line),
line => Assert.Equal(string.Empty, line),
line => Assert.Equal("Dashboard: http://localhost:18888", line),
line => Assert.Equal("Login URL: http://localhost:18888/login?t=abc123", line),
line => Assert.Equal(" - Dashboard: http://localhost:18888", line),
line => Assert.Equal(" - Login URL: http://localhost:18888/login?t=abc123", line),
line => Assert.Equal(string.Empty, line));

Assert.Null(LogTestHelpers.GetValue(write, "OtlpGrpcUrl"));
Expand All @@ -208,13 +208,9 @@ public void WriteDashboardSummary_IsContainer_IncludesContainerMessage()
token: "abc123",
isContainer: true);

Assert.Equal(2, sink.Writes.Count);

var containerWrite = sink.Writes.ElementAt(1);
Assert.NotNull(containerWrite.Message);
var containerMessage = "URLs may need changes depending on how network access to the container is configured.";

Assert.Contains(containerMessage, containerWrite.Message);
Assert.Equal(3, sink.Writes.Count);
var containerWrite = sink.Writes.Last();
Assert.Equal("Dashboard is running in a container. Access the dashboard from the host using port forwarding.", containerWrite.Message);
}

private static string[] GetMessageLines(string message)
Expand Down
Loading