Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BrowserConsoleLogger to handle different routing #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions src/Blazor.Extensions.Logging/BrowserConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;

namespace Blazor.Extensions.Logging
{
internal class BrowserConsoleLogger<T> : BrowserConsoleLogger, ILogger<T>
{
public BrowserConsoleLogger (IJSRuntime jsRuntime) : base (jsRuntime, typeof (T).FullName, null)
public BrowserConsoleLogger (IJSRuntime jsRuntime, NavigationManager navigationManager) : base (jsRuntime, navigationManager, typeof (T).FullName, null)
{
}

public BrowserConsoleLogger (IJSRuntime jsRuntime, Func<string, LogLevel, bool> filter) : base (jsRuntime, typeof (T).FullName, filter)
public BrowserConsoleLogger (IJSRuntime jsRuntime, NavigationManager navigationManager, Func<string, LogLevel, bool> filter) : base (jsRuntime, navigationManager, typeof (T).FullName, filter)
{

}
Expand All @@ -21,20 +22,20 @@ public BrowserConsoleLogger (IJSRuntime jsRuntime, Func<string, LogLevel, bool>
internal class BrowserConsoleLogger : ILogger, IAsyncDisposable
{
private const string LoggerFunctionName = "log";
private const string ScriptName = "./_content/Blazor.Extensions.Logging/BrowserConsoleLogger.js";
private const string ScriptName = "_content/Blazor.Extensions.Logging/BrowserConsoleLogger.js";

private readonly Lazy<Task<IJSObjectReference>> moduleTask;
private IJSObjectReference module;
private Func<string, LogLevel, bool> filter;

public BrowserConsoleLogger (IJSRuntime jsRuntime, string name, Func<string, LogLevel, bool> filter)
public BrowserConsoleLogger (IJSRuntime jsRuntime, NavigationManager navigationManager, string name, Func<string, LogLevel, bool> filter)
{
this.filter = filter ?? ((category, logLevel) => true);
this.Name = name ??
throw new ArgumentNullException (nameof (name));

this.moduleTask = new (() => jsRuntime.InvokeAsync<IJSObjectReference>(
"import", ScriptName).AsTask());
"import", navigationManager.ToAbsoluteUri(ScriptName)).AsTask());
}

public async void Log<TState> (LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
Expand Down
7 changes: 5 additions & 2 deletions src/Blazor.Extensions.Logging/BrowserConsoleLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.JSInterop;
using System;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Components;

namespace Blazor.Extensions.Logging
{
Expand All @@ -12,11 +13,13 @@ internal class BrowserConsoleLoggerProvider : ILoggerProvider

private readonly Func<string, LogLevel, bool> filter;
private readonly IJSRuntime runtime;
private readonly NavigationManager navigation;
private ConcurrentDictionary<string, BrowserConsoleLogger> loggers;

public BrowserConsoleLoggerProvider(IJSRuntime runtime)
public BrowserConsoleLoggerProvider(IJSRuntime runtime, NavigationManager navigation)
{
this.runtime = runtime;
this.navigation = navigation;
}

public BrowserConsoleLoggerProvider(Func<string, LogLevel, bool> filter)
Expand All @@ -41,7 +44,7 @@ public ILogger CreateLogger(string categoryName)

public void Dispose() => this.loggers?.Clear();

private BrowserConsoleLogger CreateLoggerImplementation(string name) => new BrowserConsoleLogger(this.runtime, name, this.GetFilter(name));
private BrowserConsoleLogger CreateLoggerImplementation(string name) => new BrowserConsoleLogger(this.runtime, navigation, name, this.GetFilter(name));

private Func<string, LogLevel, bool> GetFilter(string name)
{
Expand Down