From 51ff64fb2af7470f91b441b1e622bc31e4c7ce27 Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Wed, 26 Jan 2022 15:38:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Exception=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ExceptionService.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/ExceptionService.cs diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/ExceptionService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/ExceptionService.cs new file mode 100644 index 00000000..656c44f1 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/ExceptionService.cs @@ -0,0 +1,88 @@ +using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Web.Core; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BootstrapAdmin.DataAccess.EFCore.Services; + +/// +/// +/// +public class ExceptionService : IException +{ + private IDbContextFactory DbFactory; + + /// + /// + /// + /// + public ExceptionService(IDbContextFactory dbFactory) => DbFactory = dbFactory; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public (IEnumerable Items, int ItemsCount) GetAll(string? searchText, ExceptionFilter filter, int pageIndex, int pageItems, List sortList) + { + using var dbcontext = DbFactory.CreateDbContext(); + + var items = dbcontext.Set(); + + if (!string.IsNullOrEmpty(searchText)) + { + items.Where(s => s.Message!.Contains(searchText) || s.StackTrace!.Contains(searchText) || s.ErrorPage!.Contains(searchText)); + } + + if (!string.IsNullOrEmpty(filter.Category)) + { + items.Where(s => s.Category!.Contains(filter.Category)); + } + + if (!string.IsNullOrEmpty(filter.UserId)) + { + items.Where(s => s.UserId!.Contains(filter.UserId)); + } + + if (!string.IsNullOrEmpty(filter.ErrorPage)) + { + items.Where(s => s.ErrorPage!.Contains(filter.ErrorPage)); + } + + items.Where(s => s.LogTime >= filter.Star && s.LogTime <= filter.End); + + if (sortList.Any()) + { + items.Sort(sortList); + } + else + { + items.OrderByDescending(s => s.LogTime); + } + + var data = items.Take(pageItems).Skip(pageItems * (pageIndex - 1)).AsNoTracking().ToList(); + return (data, items.Count()); + } + + /// + /// + /// + /// + /// + /// + public bool Log(Error exception) + { + using var dbcontext = DbFactory.CreateDbContext(); + dbcontext.Add(exception); + return dbcontext.SaveChanges() > 0; + } +}