refactor: 增加自定义异常处理逻辑

This commit is contained in:
Argo-Tianyi 2022-01-02 16:13:36 +08:00
parent 106d85acf4
commit c83725bfa1
7 changed files with 37 additions and 12 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="6.1.2-beta06" />
<PackageReference Include="BootstrapBlazor" Version="6.1.2-beta07" />
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.1" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" />

View File

@ -43,7 +43,7 @@ public static class ServicesExtensions
[nameof(db.LastCommand)] = db.LastCommand,
[nameof(db.LastArgs)] = string.Join(",", db.LastArgs)
});
logger.LogError(e.Exception, message);
logger.LogError(new EventId(1001, "GlobalException"), e.Exception, message);
};
var env = provider.GetRequiredService<IWebHostEnvironment>();
if (env.IsDevelopment())

View File

@ -1,12 +1,20 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
class ExceptionService : IException
class ExceptionService : BaseDatabase, IException
{
public ExceptionService(IDatabase db) => Database = db;
public bool Log(Error exception)
{
try
{
Database.Insert(exception);
}
catch { }
return true;
}
}

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Longbow.Logging" Version="6.0.0" />
<PackageReference Include="Longbow.Logging" Version="6.0.2" />
<PackageReference Include="Longbow.Tasks" Version="5.2.1" />
</ItemGroup>

View File

@ -2,7 +2,7 @@
<Layout SideWidth="0" IsPage="true" IsFullSide="true" IsFixedHeader="true"
ShowFooter="true" ShowGotoTop="true" ShowCollapseBar="true" Menus="@MenuItems"
OnAuthorizing="@OnAuthorizing"
OnAuthorizing="@OnAuthorizing" OnErrorHandleAsync="OnErrorHandleAsync"
UseTabSet="true" TabDefaultUrl="/Admin/Index">
<Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span>

View File

@ -41,6 +41,10 @@ namespace BootstrapAdmin.Web.Shared
[NotNull]
private IBootstrapAdminService? SecurityService { get; set; }
[Inject]
[NotNull]
private ToastService? ToastService { get; set; }
private string? Title { get; set; }
private string? Footer { get; set; }
@ -74,5 +78,12 @@ namespace BootstrapAdmin.Web.Shared
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);
private void OnLogout() => NavigationManager.NavigateTo("/Account/Logout", true);
private async Task OnErrorHandleAsync(ILogger logger, Exception ex)
{
await ToastService.Error(Title, ex.Message);
logger.LogError(ex, "ErrorLogger");
}
}
}

View File

@ -1,4 +1,5 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using System.Collections.Specialized;
using System.Data.Common;
@ -16,11 +17,16 @@ public static class ExceptionsHelper
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
public static void Log(IServiceProvider provider, Exception? ex, NameValueCollection additionalInfo)
public static void Log(IServiceProvider provider, EventId eventId, Exception? ex, NameValueCollection additionalInfo)
{
if (ex != null)
// 1001 为 DB 异常防止循环调用
if (ex != null && eventId.Id != 1001)
{
var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name);
// 数据库长度 50 需要截取
var errorPage = ex.GetType().Name.Length > 50
? ex.GetType().Name[..50]
: ex.GetType().Name;
var loopEx = ex;
var category = "App";
while (loopEx != null)
@ -32,12 +38,12 @@ public static class ExceptionsHelper
}
loopEx = loopEx.InnerException;
}
var exception = new DataAccess.Models.Exception
var exception = new Error
{
AppDomainName = AppDomain.CurrentDomain.FriendlyName,
ErrorPage = errorPage,
UserId = additionalInfo?["UserId"],
UserIp = additionalInfo?["UserIp"],
UserId = "",
UserIp = "",
ExceptionType = ex.GetType().FullName,
Message = ex.Message,
StackTrace = ex.StackTrace,