Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 6 additions & 7 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,13 @@ 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;
// 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 the dashboard URL. When using browser token auth, include the login token.
var token = options.Frontend.AuthMode == FrontendAuthMode.BrowserToken ? options.Frontend.BrowserToken : null;
LoggingHelpers.WriteDashboardUrl(_logger, frontendEndpointInfo.GetResolvedAddress(replaceIPAnyWithLocalhost: true), token, isContainer);
}

// One-off async initialization of telemetry service.
Expand Down
15 changes: 12 additions & 3 deletions src/Shared/LoggingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ internal static class LoggingHelpers
{
public static void WriteDashboardUrl(ILogger logger, string? dashboardUrls, string? token, bool isContainer)
{
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))
if (!string.IsNullOrEmpty(token))
{
var message = !isContainer
? "Login to the dashboard at {DashboardLoginUrl}"
Expand All @@ -24,5 +24,14 @@ public static void WriteDashboardUrl(ILogger logger, string? dashboardUrls, stri
var dashboardUrl = $"{firstDashboardUrl.GetLeftPart(UriPartial.Authority)}/login?t={token}";
logger.LogInformation(message, dashboardUrl);
}
else
{
var message = !isContainer
? "The dashboard is available at {DashboardUrl}"
: "The dashboard is available at {DashboardUrl} . The URL may need changes depending on how network access to the container is configured.";
Comment thread
JamesNK marked this conversation as resolved.
Outdated

var dashboardUrl = firstDashboardUrl.GetLeftPart(UriPartial.Authority);
logger.LogInformation(message, dashboardUrl);
}
}
}
118 changes: 118 additions & 0 deletions tests/Aspire.Dashboard.Tests/LoggingHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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 WriteDashboardUrl_WithToken_LogsLoginUrl()
{
var sink = new TestSink();
var logger = new TestLogger("TestLogger", sink, enabled: true);

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888", "abc123", isContainer: false);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.Contains("/login?t=abc123", write.Message);
Assert.Contains("Login to the dashboard at", write.Message);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888", "abc123", isContainer: true);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.Contains("/login?t=abc123", write.Message);
Assert.Contains("URL may need changes depending on how network access to the container is configured", write.Message);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888", token: null, isContainer: false);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.Contains("The dashboard is available at", write.Message);
Assert.Contains("http://localhost:18888", write.Message);
Assert.DoesNotContain("/login?t=", write.Message);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888", token: null, isContainer: true);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.Contains("The dashboard is available at", write.Message);
Assert.Contains("URL may need changes depending on how network access to the container is configured", write.Message);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888", token: "", isContainer: false);

var write = Assert.Single(sink.Writes);
Assert.Equal(LogLevel.Information, write.LogLevel);
Assert.Contains("The dashboard is available at", write.Message);
Assert.DoesNotContain("/login?t=", write.Message);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "not-a-url", token: "abc123", isContainer: false);

Assert.Empty(sink.Writes);
}

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

LoggingHelpers.WriteDashboardUrl(logger, dashboardUrls: null, token: "abc123", isContainer: false);

Assert.Empty(sink.Writes);
}

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

LoggingHelpers.WriteDashboardUrl(logger, "http://localhost:18888;http://localhost:19999", "mytoken", isContainer: false);

var write = Assert.Single(sink.Writes);
Assert.Contains("http://localhost:18888/login?t=mytoken", write.Message);
Assert.DoesNotContain("19999", write.Message);
}
}
Loading