Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 16 additions & 8 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,22 @@ public DashboardWebApplication(
if (frontendEndpointInfo != null)
{
var options = _app.Services.GetRequiredService<IOptionsMonitor<DashboardOptions>>().CurrentValue;
if (options.Frontend.AuthMode == FrontendAuthMode.BrowserToken)
{
// DOTNET_RUNNING_IN_CONTAINER is a well-known environment variable added by official .NET images.
// https://learn.microsoft.com/dotnet/core/tools/dotnet-environment-variables#dotnet_running_in_container-and-dotnet_running_in_containers
var isContainer = _app.Configuration.GetBool("DOTNET_RUNNING_IN_CONTAINER") ?? false;

LoggingHelpers.WriteDashboardUrl(_logger, frontendEndpointInfo.GetResolvedAddress(replaceIPAnyWithLocalhost: true), options.Frontend.BrowserToken, isContainer);
}
// Always print a dashboard summary including dashboard and OTLP endpoints.
var token = options.Frontend.AuthMode == FrontendAuthMode.BrowserToken ? options.Frontend.BrowserToken : null;
var frontendAddress = frontendEndpointInfo.GetResolvedAddress(replaceIPAnyWithLocalhost: true);
var otlpGrpcAddress = _otlpServiceGrpcEndPointAccessor?.Invoke().GetResolvedAddress(replaceIPAnyWithLocalhost: true);
var otlpHttpAddress = _otlpServiceHttpEndPointAccessor?.Invoke().GetResolvedAddress(replaceIPAnyWithLocalhost: true);
// DOTNET_RUNNING_IN_CONTAINER is a well-known environment variable added by official .NET images.
// https://learn.microsoft.com/dotnet/core/tools/dotnet-environment-variables#dotnet_running_in_container-and-dotnet_running_in_containers
var isContainer = _app.Configuration.GetBool("DOTNET_RUNNING_IN_CONTAINER") ?? false;

LoggingHelpers.WriteDashboardSummary(
_logger,
frontendAddress,
otlpGrpcAddress,
otlpHttpAddress,
token,
isContainer);
}

// One-off async initialization of telemetry service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private bool IsEnabled()
// Explicitly disable AI in configuration.
if (_dashboardOptions.CurrentValue.AI.Disabled.GetValueOrDefault())
{
_logger.LogInformation("AI is disabled in configuration.");
_logger.LogDebug("AI is disabled in configuration.");
return false;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Aspire.Hosting/Dashboard/DashboardEventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,7 @@ private void ConfigureAspireDashboardResource(IResource dashboardResource)

distributedApplicationLogger.LogInformation("Now listening on: {DashboardUrl}", dashboardUrl.TrimEnd('/'));

if (!string.IsNullOrEmpty(browserToken))
{
LoggingHelpers.WriteDashboardUrl(distributedApplicationLogger, dashboardUrl, browserToken, isContainer: false);
}
LoggingHelpers.WriteDashboardSummary(distributedApplicationLogger, dashboardUrl, otlpGrpcEndpointUrl, otlpHttpEndpointUrl, browserToken, isContainer: false);
});

foreach (var d in dashboardUrls?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? [])
Expand Down
58 changes: 49 additions & 9 deletions src/Shared/LoggingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,66 @@

using Aspire.Hosting.Utils;
using Microsoft.Extensions.Logging;
using System.Text;

namespace Aspire.Hosting;

internal static class LoggingHelpers
{
public static void WriteDashboardUrl(ILogger logger, string? dashboardUrls, string? token, bool isContainer)
public static void WriteDashboardSummary(ILogger logger, string? dashboardUrls, string? otlpGrpcUrls, string? otlpHttpUrls, string? token, bool isContainer = false)
{
if (string.IsNullOrEmpty(token))
if (!StringUtils.TryGetUriFromDelimitedString(dashboardUrls, ";", out var firstDashboardUrl))
{
throw new InvalidOperationException("Token must be provided.");
return;
}

if (StringUtils.TryGetUriFromDelimitedString(dashboardUrls, ";", out var firstDashboardUrl))
static string? GetEndpointAuthority(string? urls)
{
var message = !isContainer
? "Login to the dashboard at {DashboardLoginUrl}"
: "Login to the dashboard at {DashboardLoginUrl} . The URL may need changes depending on how network access to the container is configured.";
return StringUtils.TryGetUriFromDelimitedString(urls, ";", out var firstUrl)
? firstUrl.GetLeftPart(UriPartial.Authority)
: null;
}

var dashboardUrl = firstDashboardUrl.GetLeftPart(UriPartial.Authority);
var otlpGrpcUrl = GetEndpointAuthority(otlpGrpcUrls);
var otlpHttpUrl = GetEndpointAuthority(otlpHttpUrls);
var loginUrl = !string.IsNullOrEmpty(token)
? $"{dashboardUrl}/login?t={token}"
: null;

var templateBuilder = new StringBuilder();
var parameters = new List<object?>();

templateBuilder
.Append("Aspire Dashboard").Append('\n')
.Append('\n')
.Append("Dashboard: {DashboardUrl}").Append('\n');
parameters.Add(dashboardUrl);

var dashboardUrl = $"{firstDashboardUrl.GetLeftPart(UriPartial.Authority)}/login?t={token}";
logger.LogInformation(message, dashboardUrl);
if (loginUrl is not null)
{
templateBuilder.Append("Login URL: {LoginUrl}").Append('\n');
parameters.Add(loginUrl);
}

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

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

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(templateBuilder.ToString(), parameters.ToArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ public async Task LogOutput_NoToken_GeneratedTokenLogged()
},
w =>
{
Assert.Equal("Login to the dashboard at {DashboardLoginUrl}", LogTestHelpers.GetValue(w, "{OriginalFormat}"));
Assert.StartsWith("Aspire Dashboard", (string)LogTestHelpers.GetValue(w, "{OriginalFormat}")!);

var uri = new Uri((string)LogTestHelpers.GetValue(w, "DashboardLoginUrl")!, UriKind.Absolute);
var loginUrl = (string)LogTestHelpers.GetValue(w, "LoginUrl")!;
var uri = new Uri(loginUrl, UriKind.Absolute);
var queryString = HttpUtility.ParseQueryString(uri.Query);
Assert.NotNull(queryString["t"]);
});
Expand All @@ -235,10 +236,11 @@ public async Task LogOutput_AnyIP_LoginLinkLocalhost(string frontendUrl, string
// Assert
var l = testSink.Writes.Where(w => w.LoggerName == typeof(DashboardWebApplication).FullName).ToList();

// Testing via the log template is kind of hacky. If this becomes a problem then consider adding proper log definitions and match via ID.
var loginLinkLog = l.Single(w => "Login to the dashboard at {DashboardLoginUrl}" == (string?)LogTestHelpers.GetValue(w, "{OriginalFormat}"));
// The login URL is now part of the summary log message.
var summaryLog = l.Single(w => ((string?)LogTestHelpers.GetValue(w, "{OriginalFormat}"))?.StartsWith("Aspire Dashboard") == true);

var uri = new Uri((string)LogTestHelpers.GetValue(loginLinkLog, "DashboardLoginUrl")!, UriKind.Absolute);
var loginUrl = (string)LogTestHelpers.GetValue(summaryLog, "LoginUrl")!;
var uri = new Uri(loginUrl, UriKind.Absolute);
var queryString = HttpUtility.ParseQueryString(uri.Query);
Assert.NotNull(queryString["t"]);

Expand All @@ -262,7 +264,9 @@ public async Task LogOutput_InContainer_LoginLinkContainerMessage()
// Assert
var l = testSink.Writes.Where(w => w.LoggerName == typeof(DashboardWebApplication).FullName).ToList();

// Testing via the log template is kind of hacky. If this becomes a problem then consider adding proper log definitions and match via ID.
Assert.Single(l, w => (string?)LogTestHelpers.GetValue(w, "{OriginalFormat}") == "Login to the dashboard at {DashboardLoginUrl} . The URL may need changes depending on how network access to the container is configured.");
// 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);
}
}
12 changes: 12 additions & 0 deletions tests/Aspire.Dashboard.Tests/Integration/StartupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ public async Task LogOutput_DynamicPort_PortResolvedInLogs()
{
Assert.Equal("Dashboard API is unsecured. Untrusted apps can access sensitive telemetry data.", LogTestHelpers.GetValue(w, "{OriginalFormat}"));
Assert.Equal(LogLevel.Warning, w.LogLevel);
},
w =>
{
Assert.StartsWith("Aspire Dashboard", (string)LogTestHelpers.GetValue(w, "{OriginalFormat}")!);
});
}

Expand Down Expand Up @@ -696,6 +700,10 @@ public async Task LogOutput_NoOtlpEndpoints_NoOtlpLogs()
{
Assert.Equal("Dashboard API is unsecured. Untrusted apps can access sensitive telemetry data.", LogTestHelpers.GetValue(w, "{OriginalFormat}"));
Assert.Equal(LogLevel.Warning, w.LogLevel);
},
w =>
{
Assert.StartsWith("Aspire Dashboard", (string)LogTestHelpers.GetValue(w, "{OriginalFormat}")!);
});
}

Expand Down Expand Up @@ -836,6 +844,10 @@ await ServerRetryHelper.BindPortsWithRetry(async ports =>
{
Assert.Equal("Dashboard API is unsecured. Untrusted apps can access sensitive telemetry data.", LogTestHelpers.GetValue(w, "{OriginalFormat}"));
Assert.Equal(LogLevel.Warning, w.LogLevel);
},
w =>
{
Assert.StartsWith("Aspire Dashboard", (string)LogTestHelpers.GetValue(w, "{OriginalFormat}")!);
});
}

Expand Down
187 changes: 187 additions & 0 deletions tests/Aspire.Dashboard.Tests/LoggingHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;

namespace Aspire.Dashboard.Tests;

public class LoggingHelpersTests
{
[Fact]
public void WriteDashboardSummary_WithTokenAndOtlpEndpoints_LogsSummaryAndStructuredUrls()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
"http://localhost:18888",
"http://localhost:18889",
"http://localhost:18890",
"abc123");

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.NotNull(write.Message);
var lines = GetMessageLines(write.Message!);

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(string.Empty, line));

Assert.Equal("http://localhost:18888", LogTestHelpers.GetValue(write, "DashboardUrl"));
Assert.Equal("http://localhost:18889", LogTestHelpers.GetValue(write, "OtlpGrpcUrl"));
Assert.Equal("http://localhost:18890", LogTestHelpers.GetValue(write, "OtlpHttpUrl"));
Assert.Equal("http://localhost:18888/login?t=abc123", LogTestHelpers.GetValue(write, "LoginUrl"));
}

[Fact]
public void WriteDashboardSummary_WithoutToken_DoesNotIncludeLoginUrl()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
"http://localhost:18888",
"http://localhost:18889",
"http://localhost:18890",
token: null);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.NotNull(write.Message);
var lines = GetMessageLines(write.Message!);

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(string.Empty, line));

Assert.Null(LogTestHelpers.GetValue(write, "LoginUrl"));
}

[Fact]
public void WriteDashboardSummary_InvalidDashboardUrl_DoesNotLog()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(logger, "not-a-url", "http://localhost:18889", "http://localhost:18890", token: "abc123");

Assert.Empty(sink.Writes);
}

[Fact]
public void WriteDashboardSummary_NullDashboardUrl_DoesNotLog()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
dashboardUrls: null,
otlpGrpcUrls: "http://localhost:18889",
otlpHttpUrls: "http://localhost:18890",
token: "abc123");

Assert.Empty(sink.Writes);
}

[Fact]
public void WriteDashboardSummary_SemicolonDelimitedUrls_UsesFirstUrls()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
"http://localhost:18888;http://localhost:19999",
"http://localhost:18889;http://localhost:19998",
"http://localhost:18890;http://localhost:19997",
"mytoken");

var write = Assert.Single(sink.Writes);
Assert.NotNull(write.Message);
var lines = GetMessageLines(write.Message!);

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=mytoken", 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"));
Assert.Equal("http://localhost:18889", LogTestHelpers.GetValue(write, "OtlpGrpcUrl"));
Assert.Equal("http://localhost:18890", LogTestHelpers.GetValue(write, "OtlpHttpUrl"));
Assert.Equal("http://localhost:18888/login?t=mytoken", LogTestHelpers.GetValue(write, "LoginUrl"));
}

[Fact]
public void WriteDashboardSummary_WithoutOtlpEndpoints_DoesNotIncludeOtlpLines()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
"http://localhost:18888",
otlpGrpcUrls: null,
otlpHttpUrls: null,
token: "abc123");

var write = Assert.Single(sink.Writes);
Assert.NotNull(write.Message);
var lines = GetMessageLines(write.Message!);

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(string.Empty, line));

Assert.Null(LogTestHelpers.GetValue(write, "OtlpGrpcUrl"));
Assert.Null(LogTestHelpers.GetValue(write, "OtlpHttpUrl"));
}

[Fact]
public void WriteDashboardSummary_IsContainer_IncludesContainerMessage()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardSummary(
logger,
"http://localhost:18888",
otlpGrpcUrls: null,
otlpHttpUrls: null,
token: "abc123",
isContainer: true);

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

Assert.Contains(containerMessage, write.Message);
}

private static string[] GetMessageLines(string message)
{
return message.Replace("\r", string.Empty).Split('\n');
}
}
Loading