diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj index d55d750f..30c963d8 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj @@ -1,7 +1,7 @@  - + diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs index 7aa41485..1cdd85ab 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs @@ -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(); if (env.IsDevelopment()) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/ExceptionService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/ExceptionService.cs index 4f5742e6..d9f2a3a1 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/ExceptionService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/ExceptionService.cs @@ -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; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj index 1dce4e71..6997d44d 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj +++ b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor index c6e5f4ab..d1097616 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor @@ -2,7 +2,7 @@
Bootstrap of Blazor diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index 43de6e2c..cb3faf60 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -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 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"); + } } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Utils/ExceptionsHelper.cs b/src/blazor/admin/BootstrapAdmin.Web/Utils/ExceptionsHelper.cs index de9682c3..a20fc449 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Utils/ExceptionsHelper.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Utils/ExceptionsHelper.cs @@ -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 /// /// /// - 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,