Skip to content

Commit b7e67a8

Browse files
authored
feat(ErrorLogger): add EnableILogger parameter (#6246)
* feat: 增加 EnableErrorLoggerILogger 配置项 * feat: 增加 EnableILogger 参数 * feat: 增加 EnableILogger 参数 * feat: 增加 EnableErrorLoggerILogger 参数 * test: 增加单元测试
1 parent d97672f commit b7e67a8

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
1818
{
1919
[Inject]
2020
[NotNull]
21-
private ILogger<ErrorLogger>? Logger { get; set; }
21+
private ILogger<BootstrapBlazorErrorBoundary>? Logger { get; set; }
2222

2323
[Inject]
2424
[NotNull]
@@ -48,6 +48,12 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
4848
[Parameter]
4949
public bool ShowToast { get; set; } = true;
5050

51+
/// <summary>
52+
/// 获得/设置 是否启用日志记录功能 默认 true 启用
53+
/// </summary>
54+
[Parameter]
55+
public bool EnableILogger { get; set; } = true;
56+
5157
/// <summary>
5258
/// 获得/设置 Toast 弹窗标题
5359
/// </summary>
@@ -61,7 +67,10 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
6167
/// <param name="exception"></param>
6268
protected override Task OnErrorAsync(Exception exception)
6369
{
64-
Logger.LogError(exception, "{BootstrapBlazorErrorBoundary} {OnErrorAsync} log this error occurred at {Page}", nameof(BootstrapBlazorErrorBoundary), nameof(OnErrorAsync), NavigationManager.Uri);
70+
if (EnableILogger)
71+
{
72+
Logger.LogError(exception, "BootstrapBlazorErrorBoundary OnErrorAsync log this error occurred at {Page}", NavigationManager.Uri);
73+
}
6574
return Task.CompletedTask;
6675
}
6776

src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public class ErrorLogger : ComponentBase, IErrorLogger
3030
[Parameter]
3131
public bool ShowToast { get; set; } = true;
3232

33+
/// <summary>
34+
/// <inheritdoc/>
35+
/// </summary>
36+
[Parameter]
37+
public bool EnableILogger { get; set; } = true;
38+
3339
/// <summary>
3440
/// <inheritdoc/>
3541
/// </summary>
@@ -112,6 +118,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
112118
builder.AddAttribute(3, nameof(BootstrapBlazorErrorBoundary.ToastTitle), ToastTitle);
113119
builder.AddAttribute(4, nameof(BootstrapBlazorErrorBoundary.ErrorContent), ErrorContent);
114120
builder.AddAttribute(5, nameof(BootstrapBlazorErrorBoundary.ChildContent), ChildContent);
121+
builder.AddAttribute(6, nameof(BootstrapBlazorErrorBoundary.EnableILogger), EnableILogger);
115122
builder.AddComponentReferenceCapture(5, obj => _errorBoundary = (BootstrapBlazorErrorBoundary)obj);
116123
builder.CloseComponent();
117124
};

src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public interface IErrorLogger
2727
/// </summary>
2828
bool ShowToast { get; }
2929

30+
/// <summary>
31+
/// 获得/设置 是否启用日志记录功能 默认 true 启用
32+
/// <para>设置 false 后关闭记录日志功能</para>
33+
/// </summary>
34+
bool EnableILogger { get; }
35+
3036
/// <summary>
3137
/// 获得 Error Toast 弹窗标题 默认读取资源文件内容
3238
/// </summary>

src/BootstrapBlazor/Components/Layout/Layout.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
else
128128
{
129129
<ErrorLogger EnableErrorLogger="@_enableErrorLogger" ShowToast="@_showToast" ToastTitle="@ErrorLoggerToastTitle"
130-
OnErrorHandleAsync="OnErrorHandleAsync" OnInitializedCallback="OnErrorLoggerInitialized">
130+
EnableILogger="@_enableILogger" OnErrorHandleAsync="OnErrorHandleAsync" OnInitializedCallback="OnErrorLoggerInitialized">
131131
@HandlerMain()
132132
</ErrorLogger>
133133
}

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ public partial class Layout : IHandlerException, ITabHeader
465465
[Parameter]
466466
public bool? ShowErrorLoggerToast { get; set; }
467467

468+
/// <summary>
469+
/// 获得/设置 是否启用日志记录功能 默认 null 启用 使用 <see cref="BootstrapBlazorOptions.EnableErrorLoggerILogger"/> 设置值
470+
/// </summary>
471+
[Parameter]
472+
public bool? EnableErrorLoggerILogger { get; set; }
473+
468474
/// <summary>
469475
/// 获得/设置 错误日志 <see cref="Toast"/> 弹窗标题 默认 null
470476
/// </summary>
@@ -503,6 +509,8 @@ public partial class Layout : IHandlerException, ITabHeader
503509

504510
private bool _showToast => ShowErrorLoggerToast ?? Options.CurrentValue.ShowErrorLoggerToast;
505511

512+
private bool _enableILogger => EnableErrorLoggerILogger ?? Options.CurrentValue.EnableErrorLoggerILogger;
513+
506514
/// <summary>
507515
/// <inheritdoc/>
508516
/// </summary>

src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.Extensions.Localization;
7+
using Microsoft.Extensions.Logging;
78
using System.Globalization;
89

910
namespace BootstrapBlazor.Components;
@@ -53,6 +54,11 @@ public class BootstrapBlazorOptions : IOptions<BootstrapBlazorOptions>
5354
/// </summary>
5455
public bool ShowErrorLoggerToast { get; set; } = true;
5556

57+
/// <summary>
58+
/// Gets or sets a value indicating whether error logging using an <see cref="ILogger"/> is enabled, default is true.
59+
/// </summary>
60+
public bool EnableErrorLoggerILogger { get; set; } = true;
61+
5662
/// <summary>
5763
/// Gets or sets whether to fall back to the fallback culture, default is true
5864
/// </summary>

test/UnitTest/Components/LayoutTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ public void ErrorLogger_LifeCycle()
568568
pb.Add(a => a.UseTabSet, false);
569569
pb.Add(a => a.EnableErrorLogger, true);
570570
pb.Add(a => a.ShowErrorLoggerToast, false);
571+
pb.Add(a => a.EnableErrorLoggerILogger, false);
571572
pb.Add(a => a.ErrorLoggerToastTitle, "Title");
572573
// 按钮触发异常
573574
pb.Add(a => a.Main, new RenderFragment(builder =>

0 commit comments

Comments
 (0)