
Global exception
Added componentErrorLogger
Through this component, global logs and exceptions can be output uniformly; currently, the Blazor
framework does not provide a MVC
like Global exception The overall solution
Instructions
1. Add AddLogging
to the Startup
file to enable the net core
system log function
AddFileLogger
need to reference Longbow.Logging component packagepublic void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder => builder.AddFileLogger());
}
2. Use BootstrapBlazorRoot
to wrap the content
<ErrorLogger>
@Body
</ErrorLogger>
3. Use cascading parameters in the code to get examples
private void OnClick()
{
var a = 0;
_ = 1 / a;
}
4. Console or IIS output visible log information
crit: BootstrapBlazor.Components.BlazorLogger[0]
TimeStamp: 8/23/2021 12:20:46 PM
MachineName: ECS-A399
AppDomainName: BootstrapBlazor.Server
OS: Microsoft Windows 10.0.17763
OSArchitecture: X64
ProcessArchitecture: X64
Framework: .NET 5.0.9
UserName: Argo
EnvironmentName: Development
IISRootPath: D:\Argo\src\BB\BootstrapBlazor\src\BootstrapBlazor.Server\
VSIDE: Microsoft Visual Studio Enterprise 2019 16.0
Exception:: Attempted to divide by zero.
*********************************************
System.DivideByZeroException: Attempted to divide by zero.
at BootstrapBlazor.Shared.Pages.GlobalException.OnClick() in D:\Argo\src\BB\BootstrapBlazor\src\BootstrapBlazor.Shared\Pages\GlobalException.razor.cs:line 30
5. Controls whether error details are displayed
appsettings.json
, which is appsettings.Development.json
in the development environment, if not set, it defaults to false
and is not displayed. The default behavior is only a pop-up prompt to prevent the exposure of sensitive information{
"DetailedErrors": true
}
This function is to obtain the component instance through the cascade parameter and use its function
Demo
In this example code, an error code that divides by zero is written. Because try/catch
is used to capture the exception, the error message is displayed in the console below
Set custom exception handling logic by setting OnErrorHandleAsync
callback method
Demo
Attributes
B station related video link
交流群