Skip to content

Commit bbaa32e

Browse files
authored
feat(ErrorLogger): check EnableErrorLogger parameter (#7483)
* refactor: 增加条件判断 * refactor: 更改编号 * feat: 响应 EnableErrorLogger 参数
1 parent 55a49c8 commit bbaa32e

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
6767
/// <param name="exception"></param>
6868
protected override Task OnErrorAsync(Exception exception)
6969
{
70-
if (EnableILogger)
70+
if (EnableILogger && Logger.IsEnabled(LogLevel.Error))
7171
{
7272
Logger.LogError(exception, "BootstrapBlazorErrorBoundary OnErrorAsync log this error occurred at {Page}", NavigationManager.Uri);
7373
}

src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -101,15 +101,20 @@ protected override async Task OnInitializedAsync()
101101
/// <param name="builder"></param>
102102
protected override void BuildRenderTree(RenderTreeBuilder builder)
103103
{
104-
builder.OpenComponent<CascadingValue<IErrorLogger>>(0);
105-
builder.AddAttribute(1, nameof(CascadingValue<>.Value), this);
106-
builder.AddAttribute(2, nameof(CascadingValue<>.IsFixed), true);
107-
builder.AddAttribute(3, nameof(CascadingValue<>.ChildContent), RenderContent);
108-
builder.CloseComponent();
104+
if (EnableErrorLogger)
105+
{
106+
builder.OpenComponent<CascadingValue<IErrorLogger>>(0);
107+
builder.AddAttribute(1, nameof(CascadingValue<>.Value), this);
108+
builder.AddAttribute(2, nameof(CascadingValue<>.IsFixed), true);
109+
builder.AddAttribute(3, nameof(CascadingValue<>.ChildContent), RenderError);
110+
builder.CloseComponent();
111+
}
112+
else
113+
{
114+
builder.AddContent(10, ChildContent);
115+
}
109116
}
110117

111-
private RenderFragment? RenderContent => EnableErrorLogger ? RenderError : ChildContent;
112-
113118
private RenderFragment RenderError => builder =>
114119
{
115120
builder.OpenComponent<BootstrapBlazorErrorBoundary>(0);
@@ -119,7 +124,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
119124
builder.AddAttribute(4, nameof(BootstrapBlazorErrorBoundary.ErrorContent), ErrorContent);
120125
builder.AddAttribute(5, nameof(BootstrapBlazorErrorBoundary.ChildContent), ChildContent);
121126
builder.AddAttribute(6, nameof(BootstrapBlazorErrorBoundary.EnableILogger), EnableILogger);
122-
builder.AddComponentReferenceCapture(5, obj => _errorBoundary = (BootstrapBlazorErrorBoundary)obj);
127+
builder.AddComponentReferenceCapture(7, obj => _errorBoundary = (BootstrapBlazorErrorBoundary)obj);
123128
builder.CloseComponent();
124129
};
125130

0 commit comments

Comments
 (0)