Enterprise-level component library based on Bootstrap and Blazor

star nuget master download license repo commit

Global exception

Added component BlazorLogger Through this component, global logs and exceptions can be output uniformly; currently, the Blazor framework does not provide a MVC like < b>Global exception The overall solution, for the time being, you need to use try/catch in the code block for exception capture

Instructions

1. Add AddLogging to the Startup file to enable the net core system log function

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(builder => builder.AddConsole());
}

2. Use BlazorLogger to wrap the content, such as: Body in MainLayout

<BlazorLogger>
    @Body
</BlazorLogger>

3. Use cascading parameters in the code to get examples

[CascadingParameter]
[NotNull]
private IBlazorLogger? Logger { get; set; }

private void OnClick()
{
    try
    {
        var a = 0;
        var b = 1 / a;
    }
    catch(Exception ex)
    {
        Logger.Log(ex);
    }
}

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

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

@page "/globalexception"
@layout ComponentLayout
@inject IStringLocalizer<GlobalException> Localizer

<h3>@Localizer["Title"]</h3>

<p>@((MarkupString)Localizer["P1"].Value)</p>

<h4>@Localizer["H1"]</h4>

<p>@((MarkupString)Localizer["P2"].Value)</p>

<Pre>public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(builder => builder.AddConsole());
}</Pre>

<p>@((MarkupString)Localizer["P3"].Value)</p>

<Pre>&lt;BlazorLogger&gt;
    @@Body
&lt;/BlazorLogger&gt;</Pre>

<p>@((MarkupString)Localizer["P4"].Value)</p>

<Pre>[CascadingParameter]
[NotNull]
private IBlazorLogger? Logger { get; set; }

private void OnClick()
{
    try
    {
        var a = 0;
        var b = 1 / a;
    }
    catch(Exception ex)
    {
        Logger.Log(ex);
    }
}</Pre>

<p>@((MarkupString)Localizer["P5"].Value)</p>
<Pre>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</Pre>

<Block Title="@Localizer["Block1Title"]" Introduction="@Localizer["Block1Intro"]">
    <p>@((MarkupString)Localizer["P6"].Value)</p>
    <Button Icon="fa fa-fa" Text="@Localizer["ButtonText"]" OnClick="OnClick" />
    <BlockLogger @ref="Trace" class="mt-3" />
</Block>
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using BootstrapBlazor.Components;
using BootstrapBlazor.Shared.Pages.Components;
using Microsoft.AspNetCore.Components;
using System;
using System.Diagnostics.CodeAnalysis;

namespace BootstrapBlazor.Shared.Pages
{
    /// <summary>
    /// 
    /// </summary>
    public partial class GlobalException
    {
        [CascadingParameter]
        [NotNull]
        private IBlazorLogger? Logger { get; set; }

        [NotNull]
        private BlockLogger? Trace { get; set; }

        private void OnClick()
        {
            try
            {
                var a = 0;
                var b = 1 / a;
            }
            catch (Exception ex)
            {
                Trace.Log(Logger.FormatException(ex).Replace(Environment.NewLine, "<br />"), true);

                // 输出到消息中心
                Logger.Log(ex);
            }
        }
    }
}

B 站相关视频链接

暂无

交流群

QQ Group:BootstrapAdmin & Blazor 795206915( Full) 675147445 Welcome to join the group discussion
Themes
Bootstrap
Ant Design (完善中)
Bluma (完善中)
LayUI (完善中)
Motronic (已集成)
An error has occurred. This application may no longer respond until reloaded. Reload