Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 0 additions & 2 deletions src/BootstrapBlazor.Server/Components/Pages/Layout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ Toast: 1090;</Pre>
<p>@Localizer["Para6"]</p>

<Button Text="@Localizer["Button"]" OnClickWithoutRender="ShowDialog"></Button>

<p class="mt-2">@Localizer["Footer1"] <code>Drawer</code> @Localizer["Footer2"]</p>
4 changes: 1 addition & 3 deletions src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1723,9 +1723,7 @@
"Para4": "本套组件额外增加了",
"Para5": "各组件分层如下",
"Para6": "测试环节",
"Button": "测试",
"Footer1": "由于",
"Footer2": "组件未提供服务形式,后期改造"
"Button": "测试"
},
"BootstrapBlazor.Server.Components.Samples.GlobalException": {
"Title": "全局异常",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
@inherits ComponentBase
@namespace BootstrapBlazor.Components

@RenderBody()
<CascadingValue Value="this" IsFixed="true">
<ErrorLogger EnableErrorLogger="EnableErrorLoggerValue" ShowToast="ShowToast" ToastTitle="@ToastTitle" OnErrorHandleAsync="OnErrorHandleAsync!">
@ChildContent

<Message @ref="MessageContainer"></Message>
<ToastContainer @ref="ToastContainer"></ToastContainer>
<DrawerContainer></DrawerContainer>
<Dialog></Dialog>
<DrawerContainer></DrawerContainer>
<SweetAlert></SweetAlert>
<Message @ref="MessageContainer"></Message>
<ToastContainer @ref="ToastContainer"></ToastContainer>

<ConnectionHub></ConnectionHub>
<Mask></Mask>
<Download></Download>
<Mask></Mask>
<Print></Print>
<ConnectionHub></ConnectionHub>

@foreach (var com in Generators)
{
@com.Generator()
}
@foreach (var com in Generators)
{
@com.Generator()
}
</ErrorLogger>
</CascadingValue>

@code {
RenderFragment RenderChildContent =>
@<CascadingValue Value="this" IsFixed="true">
@ChildContent
</CascadingValue>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,66 +78,4 @@ public override async Task SetParametersAsync(ParameterView parameters)

await base.SetParametersAsync(parameters);
}

private RenderFragment RenderBody() => builder =>
{
if (EnableErrorLoggerValue)
{
builder.OpenComponent<ErrorLogger>(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<Dialog>(0);
builder.CloseComponent();

builder.OpenComponent<SweetAlert>(2);
builder.CloseComponent();

builder.OpenComponent<Print>(3);
builder.CloseComponent();

builder.OpenComponent<Download>(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
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 40 additions & 27 deletions src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class ErrorLogger
[NotNull]
private IStringLocalizer<ErrorLogger>? Localizer { get; set; }

/// <summary>
/// 获得/设置 是否开启全局异常捕获 默认 true
/// </summary>
[Parameter]
public bool EnableErrorLogger { get; set; } = true;

/// <summary>
/// 获得/设置 是否显示弹窗 默认 true 显示
/// </summary>
Expand Down Expand Up @@ -123,24 +129,28 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(2, nameof(CascadingValue<IErrorLogger>.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<IErrorLogger>.ChildContent), content);
Expand Down Expand Up @@ -182,44 +192,47 @@ 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<ComponentBase> Cache { get; } = [];
private readonly List<ComponentBase> _cache = [];

/// <summary>
///
/// <inheritdoc/>
/// </summary>
/// <param name="component"></param>
public void Register(ComponentBase component)
{
Cache.Add(component);
_cache.Add(component);
}

/// <summary>
///
/// <inheritdoc/>
/// </summary>
/// <param name="component"></param>
public void UnRegister(ComponentBase component)
{
Cache.Remove(component);
_cache.Remove(component);
}
}
11 changes: 8 additions & 3 deletions src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
namespace BootstrapBlazor.Components;

/// <summary>
///
/// IErrorLogger 接口
/// </summary>
public interface IErrorLogger
{
/// <summary>
/// 获得/设置 是否开启全局异常捕获 默认 true
/// </summary>
bool EnableErrorLogger { get; set; }

/// <summary>
/// 自定义 Error 处理方法
/// </summary>
Expand All @@ -28,13 +33,13 @@ public interface IErrorLogger
string? ToastTitle { get; }

/// <summary>
///
/// 注册方法
/// </summary>
/// <param name="component"></param>
void Register(ComponentBase component);

/// <summary>
///
/// 注销方法
/// </summary>
/// <param name="component"></param>
void UnRegister(ComponentBase component);
Expand Down
Loading