From 5b410c7cfcf2ad5de3b0fef67e481bfc1b23132f Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:24:08 +0800 Subject: [PATCH 01/11] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Root=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseComponents/BootstrapBlazorRoot.razor | 31 ++++++---- .../BootstrapBlazorRoot.razor.cs | 62 ------------------- 2 files changed, 19 insertions(+), 74 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 1e7956982f6..18316dc08d6 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -1,23 +1,30 @@ @inherits ComponentBase @namespace BootstrapBlazor.Components -@RenderBody() - - - - + + @if (EnableErrorLoggerValue) + { + + @ChildContent + + } + else + { + @ChildContent + } + + + + + + + + @foreach (var com in Generators) { @com.Generator() } - -@code { - RenderFragment RenderChildContent => - @ - @ChildContent - ; -} diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs index 37be8e96c71..3de0a5d0eee 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs @@ -78,66 +78,4 @@ public override async Task SetParametersAsync(ParameterView parameters) await base.SetParametersAsync(parameters); } - - private RenderFragment RenderBody() => builder => - { - if (EnableErrorLoggerValue) - { - builder.OpenComponent(0); - builder.AddAttribute(1, nameof(ErrorLogger.ShowToast), ShowToast); - builder.AddAttribute(2, nameof(ErrorLogger.ToastTitle), ToastTitle); - if (OnErrorHandleAsync != null) - { - builder.AddAttribute(3, nameof(ErrorLogger.OnErrorHandleAsync), OnErrorHandleAsync); - } - builder.AddAttribute(4, nameof(ErrorLogger.ChildContent), RenderContent); - builder.CloseComponent(); - } - else - { - builder.AddContent(0, RenderContent); - } - }; - - private static RenderFragment RenderComponents() => builder => - { - builder.OpenComponent(0); - builder.CloseComponent(); - - builder.OpenComponent(2); - builder.CloseComponent(); - - builder.OpenComponent(3); - builder.CloseComponent(); - - builder.OpenComponent(4); - builder.CloseComponent(); - }; - - private RenderFragment RenderContent => builder => - { -#if NET8_0_OR_GREATER - builder.AddContent(0, RenderChildContent); - builder.AddContent(1, RenderComponents()); -#else - Render(); - - [ExcludeFromCodeCoverage] - void Render() - { - if (OperatingSystem.IsBrowser()) - { - builder.AddContent(0, RenderChildContent); - builder.AddContent(1, RenderComponents()); - } - else - { - builder.OpenElement(0, "app"); - builder.AddContent(1, RenderChildContent); - builder.CloseElement(); - builder.AddContent(2, RenderComponents()); - } - } -#endif - }; } From 4587e667f1493c459e2ff489da5569c33033b6b4 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:24:16 +0800 Subject: [PATCH 02/11] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Server/Components/Pages/Layout.razor | 2 -- src/BootstrapBlazor.Server/Locales/en-US.json | 4 +--- src/BootstrapBlazor.Server/Locales/zh-CN.json | 4 +--- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor.Server/Components/Pages/Layout.razor b/src/BootstrapBlazor.Server/Components/Pages/Layout.razor index 938ab64a582..5efd0c33605 100644 --- a/src/BootstrapBlazor.Server/Components/Pages/Layout.razor +++ b/src/BootstrapBlazor.Server/Components/Pages/Layout.razor @@ -28,5 +28,3 @@ Toast: 1090;

@Localizer["Para6"]

- -

@Localizer["Footer1"] Drawer @Localizer["Footer2"]

diff --git a/src/BootstrapBlazor.Server/Locales/en-US.json b/src/BootstrapBlazor.Server/Locales/en-US.json index aaff69b44d6..3cac30f9e08 100644 --- a/src/BootstrapBlazor.Server/Locales/en-US.json +++ b/src/BootstrapBlazor.Server/Locales/en-US.json @@ -1723,9 +1723,7 @@ "Para4": "This set of components additionally", "Para5": "The components are layered as follows", "Para6": "test session", - "Button": "test", - "Footer1": "because", - "Footer2": "The component does not provide a service form, and it is retrofitted later" + "Button": "test" }, "BootstrapBlazor.Server.Components.Samples.GlobalException": { "Title": "Global exception", diff --git a/src/BootstrapBlazor.Server/Locales/zh-CN.json b/src/BootstrapBlazor.Server/Locales/zh-CN.json index d682c79862a..9e017af5d04 100644 --- a/src/BootstrapBlazor.Server/Locales/zh-CN.json +++ b/src/BootstrapBlazor.Server/Locales/zh-CN.json @@ -1723,9 +1723,7 @@ "Para4": "本套组件额外增加了", "Para5": "各组件分层如下", "Para6": "测试环节", - "Button": "测试", - "Footer1": "由于", - "Footer2": "组件未提供服务形式,后期改造" + "Button": "测试" }, "BootstrapBlazor.Server.Components.Samples.GlobalException": { "Title": "全局异常", From 18c56ef057c79c5e328aa8bf04f7a7816d84e8cf Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:38:43 +0800 Subject: [PATCH 03/11] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Root=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseComponents/BootstrapBlazorRoot.razor | 41 ++++++++----------- .../Components/ErrorLogger/ErrorLogger.cs | 14 +++---- .../Components/ErrorLogger/IErrorLogger.cs | 6 +-- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 18316dc08d6..a9038b07a91 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -2,29 +2,22 @@ @namespace BootstrapBlazor.Components - @if (EnableErrorLoggerValue) - { - - @ChildContent - - } - else - { - @ChildContent - } - + + + + + + + + + + + - - - - - - - - - + @foreach (var com in Generators) + { + @com.Generator() + } + + -@foreach (var com in Generators) -{ - @com.Generator() -} diff --git a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs index a151eed66fd..98160e07a7f 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs @@ -130,9 +130,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) #endif if (ex != null && ErrorContent != null) { - if (Cache.Count > 0) + if (_cache.Count > 0) { - var component = Cache.Last(); + var component = _cache.Last(); if (component is IHandlerException handler) { handler.HandlerException(ex, ErrorContent); @@ -203,23 +203,23 @@ protected async Task OnErrorAsync(Exception exception) } } - private List Cache { get; } = []; + private List _cache = []; /// - /// + /// /// /// public void Register(ComponentBase component) { - Cache.Add(component); + _cache.Add(component); } /// - /// + /// /// /// public void UnRegister(ComponentBase component) { - Cache.Remove(component); + _cache.Remove(component); } } diff --git a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs index 218a4ea5f95..4c8436eced8 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs @@ -6,7 +6,7 @@ namespace BootstrapBlazor.Components; /// -/// +/// IErrorLogger 接口 /// public interface IErrorLogger { @@ -28,13 +28,13 @@ public interface IErrorLogger string? ToastTitle { get; } /// - /// + /// 注册方法 /// /// void Register(ComponentBase component); /// - /// + /// 注销方法 /// /// void UnRegister(ComponentBase component); From a7ec1843c408fd42cbca521c979b038953b5b668 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Wed, 30 Oct 2024 16:21:10 +0800 Subject: [PATCH 04/11] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20EnableEr?= =?UTF-8?q?rorLogger=20=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/ErrorLogger/ErrorLogger.cs | 59 +++++++++++-------- .../Components/ErrorLogger/IErrorLogger.cs | 5 ++ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs index 98160e07a7f..98d6e0201dc 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs @@ -37,6 +37,12 @@ public class ErrorLogger [NotNull] private IStringLocalizer? Localizer { get; set; } + /// + /// 获得/设置 是否开启全局异常捕获 默认 true + /// + [Parameter] + public bool EnableErrorLogger { get; set; } = true; + /// /// 获得/设置 是否显示弹窗 默认 true 显示 /// @@ -123,24 +129,28 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) builder.AddAttribute(2, nameof(CascadingValue.IsFixed), true); var content = ChildContent; + + if (EnableErrorLogger) + { #if NET6_0_OR_GREATER - var ex = Exception ?? CurrentException; + var ex = Exception ?? CurrentException; #else var ex = Exception; #endif - if (ex != null && ErrorContent != null) - { - if (_cache.Count > 0) + if (ex != null && ErrorContent != null) { - var component = _cache.Last(); - if (component is IHandlerException handler) + if (_cache.Count > 0) { - handler.HandlerException(ex, ErrorContent); + var component = _cache.Last(); + if (component is IHandlerException handler) + { + handler.HandlerException(ex, ErrorContent); + } + } + else + { + content = ErrorContent.Invoke(ex); } - } - else - { - content = ErrorContent.Invoke(ex); } } builder.AddAttribute(3, nameof(CascadingValue.ChildContent), content); @@ -182,28 +192,31 @@ protected override async Task OnErrorAsync(Exception exception) protected async Task OnErrorAsync(Exception exception) #endif { - // 由框架调用 - if (OnErrorHandleAsync != null) + if (EnableErrorLogger) { - await OnErrorHandleAsync(Logger, exception); - } - else - { - if (ShowToast) + // 由框架调用 + if (OnErrorHandleAsync != null) { - await ToastService.Error(ToastTitle, exception.Message); + await OnErrorHandleAsync(Logger, exception); } + else + { + if (ShowToast) + { + await ToastService.Error(ToastTitle, exception.Message); + } #if NET6_0_OR_GREATER - // 此处注意 内部 logLevel=Warning - await ErrorBoundaryLogger.LogErrorAsync(exception); + // 此处注意 内部 logLevel=Warning + await ErrorBoundaryLogger.LogErrorAsync(exception); #else - Logger.LogError(exception, ""); + Logger.LogError(exception, ""); #endif + } } } - private List _cache = []; + private readonly List _cache = []; /// /// diff --git a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs index 4c8436eced8..19005dc674a 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs @@ -10,6 +10,11 @@ namespace BootstrapBlazor.Components; /// public interface IErrorLogger { + /// + /// 获得/设置 是否开启全局异常捕获 默认 true + /// + bool EnableErrorLogger { get; set; } + /// /// 自定义 Error 处理方法 /// From 40f2caa9e8b7e9c04609da4134516c484dbcb1dc Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Wed, 30 Oct 2024 16:21:32 +0800 Subject: [PATCH 05/11] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20Root=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=A2=9E=E5=8A=A0=20EnableErrorLogger=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/BaseComponents/BootstrapBlazorRoot.razor | 4 +++- .../Components/BaseComponents/BootstrapComponentBase.cs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index a9038b07a91..d6c316e6978 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -2,7 +2,9 @@ @namespace BootstrapBlazor.Components - + + @ChildContent + diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapComponentBase.cs b/src/BootstrapBlazor/Components/BaseComponents/BootstrapComponentBase.cs index 5bbfc6ea521..26a0c22983f 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapComponentBase.cs +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapComponentBase.cs @@ -52,7 +52,7 @@ private async Task CallStateHasChangedOnAsyncCompletion(Task task) return; } - if (ErrorLogger != null) + if (ErrorLogger is { EnableErrorLogger: true }) { IsNotRender = true; await ErrorLogger.HandlerExceptionAsync(ex); From 920e911f5e0954e4407f8b7558a005496b092026 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Wed, 30 Oct 2024 19:14:58 +0800 Subject: [PATCH 06/11] =?UTF-8?q?test:=20=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/ErrorHandlerTest.cs | 1 + test/UnitTest/Components/ErrorLoggerTest.cs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/UnitTest/Components/ErrorHandlerTest.cs b/test/UnitTest/Components/ErrorHandlerTest.cs index 53efee45af8..2c4e12520e5 100644 --- a/test/UnitTest/Components/ErrorHandlerTest.cs +++ b/test/UnitTest/Components/ErrorHandlerTest.cs @@ -45,6 +45,7 @@ private class MockDialogTest : ComponentBase [NotNull] public DialogService? DialogService { get; set; } } + private class ErrorComponent : ComponentBase { protected override void BuildRenderTree(RenderTreeBuilder builder) diff --git a/test/UnitTest/Components/ErrorLoggerTest.cs b/test/UnitTest/Components/ErrorLoggerTest.cs index aeaed7e2dc8..d89f0760682 100644 --- a/test/UnitTest/Components/ErrorLoggerTest.cs +++ b/test/UnitTest/Components/ErrorLoggerTest.cs @@ -79,7 +79,7 @@ public void OnErrorHandleAsync_Tab() builder.AddAttribute(1, nameof(TabItem.ChildContent), new RenderFragment(builder => { builder.OpenComponent