-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
22 changed files
with
364 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 27 additions & 3 deletions
30
...it.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/ILoggingBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
namespace Microsoft.Extensions.Logging; | ||
using Boilerplate.Client.Core.Services.DiagnosticLog; | ||
|
||
namespace Microsoft.Extensions.Logging; | ||
|
||
public static class ILoggingBuilderExtensions | ||
{ | ||
public static ILoggingBuilder AddBrowserConsoleLogger(this ILoggingBuilder builder) | ||
public static ILoggingBuilder AddDiagnosticLogger(this ILoggingBuilder builder) | ||
{ | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, BrowserConsoleLoggerProvider>()); | ||
builder.Services.AddSessioned<DiagnosticLogger>(); | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, DiagnosticLoggerProvider>()); | ||
|
||
return builder; | ||
} | ||
|
||
public static ILoggingBuilder ConfigureLoggers(this ILoggingBuilder loggingBuilder) | ||
{ | ||
loggingBuilder.ClearProviders(); | ||
|
||
if (AppEnvironment.IsDev()) | ||
{ | ||
loggingBuilder.AddDebug(); | ||
} | ||
|
||
if (!AppPlatform.IsBrowser) | ||
{ | ||
loggingBuilder.AddConsole(); | ||
// DiagnosticLogger is already logging in browser's console. | ||
// But Console logger is still useful in Visual Studio's Device Log (Android, iOS) or BrowserStack etc. | ||
} | ||
|
||
loggingBuilder.AddDiagnosticLogger(); | ||
|
||
return loggingBuilder; | ||
} | ||
} |
90 changes: 0 additions & 90 deletions
90
...t.Boilerplate/src/Client/Boilerplate.Client.Core/Services/BrowserConsoleLoggerProvider.cs
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
...Boilerplate/src/Client/Boilerplate.Client.Core/Services/Contracts/CurrentScopeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Boilerplate.Client.Core.Components; | ||
|
||
namespace Boilerplate.Client.Core.Services.Contracts; | ||
|
||
/// <summary> | ||
/// Provides the current scope's <see cref="IServiceProvider"/>. | ||
/// | ||
/// In different hosting environments, this delegate returns the `IServiceProvider` from: | ||
/// | ||
/// - **Blazor Server, SSR, and Pre-rendering:** `HttpContextAccessor.HttpContext.RequestServices` | ||
/// - **Blazor WebAssembly and Hybrid:** Gets it from <see cref="ClientAppCoordinator.CurrentServiceProvider"/> | ||
/// | ||
/// The delegate may return `null` in the following scenarios: | ||
/// | ||
/// - When there's no active `HttpContext` in backend environments. | ||
/// - When the Routes.razor page is not loaded yet. | ||
/// </summary> | ||
/// <returns>The current scope's `IServiceProvider`, or `null` if not available.</returns> | ||
public delegate IServiceProvider? CurrentScopeProvider(); |
78 changes: 78 additions & 0 deletions
78
...Boilerplate/src/Client/Boilerplate.Client.Core/Services/DiagnosticLog/DiagnosticLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Boilerplate.Client.Core.Services.DiagnosticLog; | ||
|
||
public partial class DiagnosticLogger(CurrentScopeProvider scopeProvider) : ILogger, IDisposable | ||
{ | ||
private ConcurrentQueue<IDictionary<string, object?>> states = new(); | ||
|
||
public string? CategoryName { get; set; } | ||
|
||
public IDisposable? BeginScope<TState>(TState state) | ||
where TState : notnull | ||
{ | ||
if (state is IDictionary<string, object?> data) | ||
{ | ||
data[nameof(CategoryName)] = CategoryName; | ||
states.Enqueue(data); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return logLevel != LogLevel.None; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
if (IsEnabled(logLevel) is false) return; | ||
|
||
var message = formatter(state, exception); | ||
|
||
states.TryDequeue(out var currentState); | ||
|
||
var scope = scopeProvider.Invoke(); | ||
|
||
if (scope is null) | ||
return; | ||
|
||
// Store logs in the memory to be shown later. | ||
|
||
var jsRuntime = scope.GetRequiredService<IJSRuntime>(); | ||
|
||
if (jsRuntime.IsInitialized() is false) | ||
return; | ||
|
||
var console = scope.GetRequiredService<Bit.Butil.Console>(); | ||
|
||
switch (logLevel) | ||
{ | ||
case LogLevel.Trace: | ||
case LogLevel.Debug: | ||
console!.Log(message, $"{Environment.NewLine}Category:", CategoryName, $"{Environment.NewLine}State:", currentState); | ||
break; | ||
case LogLevel.Information: | ||
console!.Info(message, $"{Environment.NewLine}Category:", CategoryName, $"{Environment.NewLine}State:", currentState); | ||
break; | ||
case LogLevel.Warning: | ||
console!.Warn(message, $"{Environment.NewLine}Category:", CategoryName, $"{Environment.NewLine}State:", currentState); | ||
break; | ||
case LogLevel.Error: | ||
case LogLevel.Critical: | ||
console!.Error(message, $"{Environment.NewLine}Category:", CategoryName, $"{Environment.NewLine}State:", currentState); | ||
break; | ||
case LogLevel.None: | ||
break; | ||
default: | ||
console!.Log(message, $"{Environment.NewLine}Category:", CategoryName, $"{Environment.NewLine}State:", currentState); | ||
break; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
|
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ate/src/Client/Boilerplate.Client.Core/Services/DiagnosticLog/DiagnosticLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//+:cnd:noEmit | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Boilerplate.Client.Core.Services.DiagnosticLog; | ||
|
||
// https://learn.microsoft.com/en-us/aspnet/core/blazor/hybrid/developer-tools | ||
|
||
/// <summary> | ||
/// Provides a custom logger that outputs log messages to the browser's console and allows for selective display of logs | ||
/// within the application UI for enhanced diagnostics. | ||
/// </summary> | ||
[ProviderAlias("DevInsights")] | ||
public partial class DiagnosticLoggerProvider : ILoggerProvider | ||
{ | ||
[AutoInject] private CurrentScopeProvider scopeProvider = default!; | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new DiagnosticLogger(scopeProvider) | ||
{ | ||
CategoryName = categoryName | ||
}; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.