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
35 changes: 22 additions & 13 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,7 @@ public DashboardWebApplication(
_logger.LogWarning("Dashboard API is unsecured. Untrusted apps can access sensitive telemetry data.");
}

// Log frontend login URL last at startup so it's easy to find in the logs.
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);
}
}
PrintSummary(frontendEndpointInfo);

// One-off async initialization of telemetry service.
var telemetryService = _app.Services.GetRequiredService<DashboardTelemetryService>();
Expand Down Expand Up @@ -535,6 +523,27 @@ public DashboardWebApplication(
_app.MapDashboardHealthChecks();
}

private void PrintSummary(ResolvedEndpointInfo? frontendEndpointInfo)
{
var options = _app.Services.GetRequiredService<IOptionsMonitor<DashboardOptions>>().CurrentValue;
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);
}

private ILogger<DashboardWebApplication> GetLogger()
{
return _app.Services.GetRequiredService<ILoggerFactory>().CreateLogger<DashboardWebApplication>();
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
77 changes: 67 additions & 10 deletions src/Shared/LoggingHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,85 @@
// 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.Utils;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
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? dashboardUrl, string? otlpGrpcUrl, string? otlpHttpUrl, string? token, bool isContainer = false)
{
if (string.IsNullOrEmpty(token))
// Callers should pass a single resolved URL, not a semicolon-delimited list.
AssertSingleUrl(dashboardUrl, nameof(dashboardUrl));
AssertSingleUrl(otlpGrpcUrl, nameof(otlpGrpcUrl));
AssertSingleUrl(otlpHttpUrl, nameof(otlpHttpUrl));

static string? GetAuthority(string? url)
{
return Uri.TryCreate(url, UriKind.Absolute, out var uri)
? uri.GetLeftPart(UriPartial.Authority)
: null;
}

var dashboardAuthority = GetAuthority(dashboardUrl);
var otlpGrpcAuthority = GetAuthority(otlpGrpcUrl);
var otlpHttpAuthority = GetAuthority(otlpHttpUrl);

// Nothing to log if we have no URLs at all.
if (dashboardAuthority is null && otlpGrpcAuthority is null && otlpHttpAuthority is null)
{
throw new InvalidOperationException("Token must be provided.");
return;
}

if (StringUtils.TryGetUriFromDelimitedString(dashboardUrls, ";", out var firstDashboardUrl))
var loginUrl = !string.IsNullOrEmpty(token) && dashboardAuthority is not null
? $"{dashboardAuthority}/login?t={token}"
: null;

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

templateBuilder
.Append("Aspire Dashboard").Append('\n')
.Append('\n');

if (dashboardAuthority is not null)
{
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.";
templateBuilder.Append("Dashboard: {DashboardUrl}").Append('\n');
parameters.Add(dashboardAuthority);
}

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 (otlpGrpcAuthority is not null)
{
templateBuilder.Append("OTLP/gRPC: {OtlpGrpcUrl}").Append('\n');
parameters.Add(otlpGrpcAuthority);
}

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

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());
}

[Conditional("DEBUG")]
private static void AssertSingleUrl(string? url, string paramName)
{
Debug.Assert(url is null || !url.Contains(';'), $"{paramName} should not contain ';': {url}");
}
}
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
Loading
Loading