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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ internal static class BrowserNavigationManagerInterop

public const string NavigateTo = Prefix + "navigateTo";

public const string NavigateToWithArgs = Prefix + "navigateToWithArgs";

public const string Refresh = Prefix + "refresh";

public const string SetHasLocationChangingListeners = Prefix + "setHasLocationChangingListeners";
Expand Down
5 changes: 0 additions & 5 deletions src/Components/Web.JS/src/Services/NavigationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const internalFunctions = {
setHasLocationChangingListeners,
endLocationChanging,
navigateTo: navigateToFromDotNet,
navigateToWithArgs: navigateToFromDotNetWithArgs,
refresh,
getBaseURI: (): string => document.baseURI,
getLocationHref: (): string => location.href,
Expand Down Expand Up @@ -116,10 +115,6 @@ function navigateToFromDotNet(uri: string, options: NavigationOptions): void {
navigateToCore(uri, options, /* skipLocationChangingCallback */ true);
}

function navigateToFromDotNetWithArgs(uri: string, forceLoad: boolean, replaceHistoryEntry: boolean, historyEntryState: string | null): void {
navigateToCore(uri, { forceLoad, replaceHistoryEntry, historyEntryState: historyEntryState ?? undefined }, /* skipLocationChangingCallback */ true);
}

function navigateToCore(uri: string, options: NavigationOptions, skipLocationChangingCallback = false): void {
const absoluteUri = toAbsoluteUri(uri);
const pageLoadMechanism = currentPageLoadMechanism();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ internal void InitializeDefaultServices()
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(Services, RenderMode.InteractiveWebAssembly);
Services.AddLogging(builder =>
{
builder.AddProvider(new WebAssemblyConsoleLoggerProvider());
builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
});
Services.AddSingleton<AntiforgeryStateProvider, DefaultAntiforgeryStateProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<AntiforgeryStateProvider>(Services, RenderMode.InteractiveWebAssembly);
Services.AddSupplyValueFromQueryProvider();

Comment thread
pavelsavara marked this conversation as resolved.
// Register metrics and tracing when explicitly enabled (opt-in via feature switch)
var isTelemetryEnabled = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out var switchValue) && switchValue == true;
if (isTelemetryEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using Microsoft.JSInterop.WebAssembly;

namespace Microsoft.AspNetCore.Components.WebAssembly.Services;

Expand All @@ -16,15 +18,17 @@ internal sealed class WebAssemblyConsoleLogger<T> : ILogger<T>, ILogger
private static readonly StringBuilder _logBuilder = new StringBuilder();

private readonly string _name;
private readonly WebAssemblyJSRuntime _jsRuntime;

public WebAssemblyConsoleLogger()
: this(string.Empty)
public WebAssemblyConsoleLogger(IJSRuntime jsRuntime)
: this(string.Empty, (WebAssemblyJSRuntime)jsRuntime) // Cast for DI
{
}

public WebAssemblyConsoleLogger(string name)
public WebAssemblyConsoleLogger(string name, WebAssemblyJSRuntime jsRuntime)
{
_name = name ?? throw new ArgumentNullException(nameof(name));
_jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
Comment thread
pavelsavara marked this conversation as resolved.
}

public IDisposable? BeginScope<TState>(TState state) where TState : notnull
Expand Down Expand Up @@ -54,7 +58,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}
}

private static void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception? exception)
private void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception? exception)
{
lock (_logBuilder)
{
Expand Down Expand Up @@ -89,7 +93,7 @@ private static void WriteMessage(LogLevel logLevel, string logName, int eventId,
break;
default: // invalid enum values
Debug.Assert(logLevel != LogLevel.None, "This method is never called with LogLevel.None.");
ConsoleLoggerInterop.ConsoleLog(formattedMessage);
_jsRuntime.InvokeVoid("console.log", formattedMessage);
break;
}
}
Expand Down Expand Up @@ -162,8 +166,6 @@ public void Dispose() { }

internal static partial class ConsoleLoggerInterop
{
[JSImport("globalThis.console.log")]
public static partial void ConsoleLog(string message);
[JSImport("globalThis.console.debug")]
public static partial void ConsoleDebug(string message);
[JSImport("globalThis.console.info")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop.WebAssembly;

namespace Microsoft.AspNetCore.Components.WebAssembly.Services;

Expand All @@ -11,12 +12,22 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Services;
/// </summary>
internal sealed class WebAssemblyConsoleLoggerProvider : ILoggerProvider
{
private readonly ConcurrentDictionary<string, WebAssemblyConsoleLogger<object>> _loggers = new();
private readonly ConcurrentDictionary<string, WebAssemblyConsoleLogger<object>> _loggers;
private readonly WebAssemblyJSRuntime _jsRuntime;

/// <summary>
/// Creates an instance of <see cref="WebAssemblyConsoleLoggerProvider"/>.
/// </summary>
public WebAssemblyConsoleLoggerProvider(WebAssemblyJSRuntime jsRuntime)
{
_loggers = new ConcurrentDictionary<string, WebAssemblyConsoleLogger<object>>();
_jsRuntime = jsRuntime;
}

/// <inheritdoc />
public ILogger CreateLogger(string name)
{
return _loggers.GetOrAdd(name, static loggerName => new WebAssemblyConsoleLogger<object>(loggerName));
return _loggers.GetOrAdd(name, loggerName => new WebAssemblyConsoleLogger<object>(name, _jsRuntime));
Comment thread
pavelsavara marked this conversation as resolved.
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices.JavaScript;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using Interop = Microsoft.AspNetCore.Components.Web.BrowserNavigationManagerInterop;

namespace Microsoft.AspNetCore.Components.WebAssembly.Services;

Expand Down Expand Up @@ -48,6 +49,7 @@ public async ValueTask<bool> HandleLocationChangingAsync(string uri, string? sta
}

/// <inheritdoc />
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(NavigationOptions))]
protected override void NavigateToCore(string uri, NavigationOptions options)
{
ArgumentNullException.ThrowIfNull(uri);
Expand All @@ -66,7 +68,7 @@ async Task PerformNavigationAsync()
return;
}

NavigationManagerInterop.NavigateTo(uri, options.ForceLoad, options.ReplaceHistoryEntry, options.HistoryEntryState);
DefaultWebAssemblyJSRuntime.Instance.InvokeVoid(Interop.NavigateTo, uri, options);
}
catch (Exception ex)
{
Expand All @@ -80,7 +82,7 @@ async Task PerformNavigationAsync()
/// <inheritdoc />
public override void Refresh(bool forceReload = false)
{
NavigationManagerInterop.Refresh(forceReload);
DefaultWebAssemblyJSRuntime.Instance.InvokeVoid(Interop.Refresh, forceReload);
}

protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context)
Expand All @@ -100,12 +102,3 @@ private static partial class Log
public static partial void NavigationFailed(ILogger logger, string uri, Exception exception);
}
}

internal static partial class NavigationManagerInterop
{
[JSImport(BrowserNavigationManagerInterop.NavigateToWithArgs, "blazor-internal")]
public static partial void NavigateTo(string uri, bool forceLoad, bool replaceHistoryEntry, string? historyEntryState);

[JSImport(BrowserNavigationManagerInterop.Refresh, "blazor-internal")]
public static partial void Refresh(bool forceReload);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.WebAssembly.Services;
using Microsoft.JSInterop;

namespace BasicTestApp;

Expand All @@ -13,10 +14,10 @@ internal class PrependMessageLoggerProvider : ILoggerProvider
readonly ILogger _defaultLogger;
private bool _disposed = false;

public PrependMessageLoggerProvider(string message)
public PrependMessageLoggerProvider(string message, IJSRuntime runtime)
{
_message = message;
_defaultLogger = new WebAssemblyConsoleLogger<object>();
_defaultLogger = new WebAssemblyConsoleLogger<object>(runtime);
}
Comment thread
pavelsavara marked this conversation as resolved.

public ILogger CreateLogger(string categoryName)
Expand Down
4 changes: 2 additions & 2 deletions src/Components/test/testassets/BasicTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static async Task Main(string[] args)

builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));

builder.Logging.Services.AddSingleton<ILoggerProvider, PrependMessageLoggerProvider>(_ =>
new PrependMessageLoggerProvider(builder.Configuration["Logging:PrependMessage:Message"]));
builder.Logging.Services.AddSingleton<ILoggerProvider, PrependMessageLoggerProvider>(s =>
new PrependMessageLoggerProvider(builder.Configuration["Logging:PrependMessage:Message"], s.GetService<IJSRuntime>()));
Comment thread
pavelsavara marked this conversation as resolved.

var host = builder.Build();
ConfigureCulture(host);
Expand Down
Loading