From 96ae77e5ab05c5b41be1c6b3bb70c9b3a4453007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Tue, 23 Jan 2024 11:52:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=90=AD=E5=BB=BA=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=AE=A1=E8=AE=A1=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SqlSugarLogAuditingStore.cs | 27 --- .../AuditLogInfoToAuditLogConverter.cs | 102 ++++++++++ .../AuditingStore.cs | 61 ++++++ .../{AuditLog.cs => AuditLogAggregateRoot.cs} | 0 .../EntityChangeWithUsername.cs | 10 + .../IAuditLogInfoToAuditLogConverter.cs | 10 + .../Repositories/IAuditLogRepository.cs | 19 ++ .../Yi.Framework.AuditLogging.Domain.csproj | 1 + .../YiFrameworkAuditLoggingDomainModule.cs | 14 +- .../Entities/AuditLogEntity.cs | 14 -- .../SqlSugarCoreAuditLogRepository.cs | 177 ++++-------------- ...Framework.AuditLogging.SqlSugarCore.csproj | 5 +- .../YiAuditLoggingDbContext.cs | 130 ------------- ...FrameworkAuditLoggingSqlSugarCoreModule.cs | 10 +- 14 files changed, 255 insertions(+), 325 deletions(-) delete mode 100644 Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditLogInfoToAuditLogConverter.cs create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditingStore.cs rename Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Entities/{AuditLog.cs => AuditLogAggregateRoot.cs} (100%) create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/EntityChangeWithUsername.cs create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/IAuditLogInfoToAuditLogConverter.cs create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Repositories/IAuditLogRepository.cs delete mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs rename Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/{ => Repositories}/SqlSugarCoreAuditLogRepository.cs (56%) delete mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs deleted file mode 100644 index b720c9fd..00000000 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Volo.Abp.Auditing; -using Volo.Abp.DependencyInjection; -using Yi.Framework.Core.Helper; -using Yi.Framework.SqlSugarCore.Abstractions; - -namespace Yi.Framework.SqlSugarCore -{ - public class SqlSugarLogAuditingStore : IAuditingStore, ISingletonDependency - { - private readonly ILogger _logger; - public SqlSugarLogAuditingStore(ILogger logger, ISqlSugarDbContext sqlSugarDbContext) - { - _logger= logger; - } - public Task SaveAsync(AuditLogInfo auditInfo) - { - _logger.LogDebug("Yi-请求追踪:"+JsonHelper.ObjToStr(auditInfo, "yyyy-MM-dd HH:mm:ss")); - return Task.CompletedTask; - } - } -} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditLogInfoToAuditLogConverter.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditLogInfoToAuditLogConverter.cs new file mode 100644 index 00000000..94f392f3 --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditLogInfoToAuditLogConverter.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.Auditing; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; +using Volo.Abp.Http; +using Volo.Abp.Json; +using Yi.Framework.AuditLogging.Domain.Entities; + +namespace Yi.Framework.AuditLogging.Domain; + +public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter, ITransientDependency +{ + protected IGuidGenerator GuidGenerator { get; } + protected IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; } + protected IJsonSerializer JsonSerializer { get; } + protected AbpExceptionHandlingOptions ExceptionHandlingOptions { get; } + + public AuditLogInfoToAuditLogConverter(IGuidGenerator guidGenerator, IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, IJsonSerializer jsonSerializer, IOptions exceptionHandlingOptions) + { + GuidGenerator = guidGenerator; + ExceptionToErrorInfoConverter = exceptionToErrorInfoConverter; + JsonSerializer = jsonSerializer; + ExceptionHandlingOptions = exceptionHandlingOptions.Value; + } + + public virtual Task ConvertAsync(AuditLogInfo auditLogInfo) + { + var auditLogId = GuidGenerator.Create(); + + var extraProperties = new ExtraPropertyDictionary(); + if (auditLogInfo.ExtraProperties != null) + { + foreach (var pair in auditLogInfo.ExtraProperties) + { + extraProperties.Add(pair.Key, pair.Value); + } + } + + var entityChanges = auditLogInfo + .EntityChanges? + .Select(entityChangeInfo => new EntityChangeEntity(GuidGenerator, auditLogId, entityChangeInfo, tenantId: auditLogInfo.TenantId)) + .ToList() + ?? new List(); + + var actions = auditLogInfo + .Actions? + .Select(auditLogActionInfo => new AuditLogActionEntity(GuidGenerator.Create(), auditLogId, auditLogActionInfo, tenantId: auditLogInfo.TenantId)) + .ToList() + ?? new List(); + + var remoteServiceErrorInfos = auditLogInfo.Exceptions?.Select(exception => ExceptionToErrorInfoConverter.Convert(exception, options => + { + options.SendExceptionsDetailsToClients = ExceptionHandlingOptions.SendExceptionsDetailsToClients; + options.SendStackTraceToClients = ExceptionHandlingOptions.SendStackTraceToClients; + })) + ?? new List(); + + var exceptions = remoteServiceErrorInfos.Any() + ? JsonSerializer.Serialize(remoteServiceErrorInfos, indented: true) + : null; + + var comments = auditLogInfo + .Comments? + .JoinAsString(Environment.NewLine); + + var auditLog = new AuditLogAggregateRoot( + auditLogId, + auditLogInfo.ApplicationName, + auditLogInfo.TenantId, + auditLogInfo.TenantName, + auditLogInfo.UserId, + auditLogInfo.UserName, + auditLogInfo.ExecutionTime, + auditLogInfo.ExecutionDuration, + auditLogInfo.ClientIpAddress, + auditLogInfo.ClientName, + auditLogInfo.ClientId, + auditLogInfo.CorrelationId, + auditLogInfo.BrowserInfo, + auditLogInfo.HttpMethod, + auditLogInfo.Url, + auditLogInfo.HttpStatusCode, + auditLogInfo.ImpersonatorUserId, + auditLogInfo.ImpersonatorUserName, + auditLogInfo.ImpersonatorTenantId, + auditLogInfo.ImpersonatorTenantName, + extraProperties, + entityChanges, + actions, + exceptions, + comments + ); + + return Task.FromResult(auditLog); + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditingStore.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditingStore.cs new file mode 100644 index 00000000..c850e609 --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditingStore.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Volo.Abp.Auditing; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Uow; +using Yi.Framework.AuditLogging.Domain.Repositories; +using Yi.Framework.Core.Helper; + +namespace Yi.Framework.AuditLogging.Domain; + +public class AuditingStore : IAuditingStore, ITransientDependency +{ + public ILogger Logger { get; set; } + protected IAuditLogRepository AuditLogRepository { get; } + protected IUnitOfWorkManager UnitOfWorkManager { get; } + protected AbpAuditingOptions Options { get; } + protected IAuditLogInfoToAuditLogConverter Converter { get; } + public AuditingStore( + IAuditLogRepository auditLogRepository, + IUnitOfWorkManager unitOfWorkManager, + IOptions options, + IAuditLogInfoToAuditLogConverter converter) + { + AuditLogRepository = auditLogRepository; + UnitOfWorkManager = unitOfWorkManager; + Converter = converter; + Options = options.Value; + + Logger = NullLogger.Instance; + } + + public virtual async Task SaveAsync(AuditLogInfo auditInfo) + { + if (!Options.HideErrors) + { + await SaveLogAsync(auditInfo); + return; + } + + try + { + await SaveLogAsync(auditInfo); + } + catch (Exception ex) + { + Logger.LogWarning("Could not save the audit log object: " + Environment.NewLine + auditInfo.ToString()); + Logger.LogException(ex, LogLevel.Error); + } + } + + protected virtual async Task SaveLogAsync(AuditLogInfo auditInfo) + { + Logger.LogDebug("Yi-请求追踪:" + JsonHelper.ObjToStr(auditInfo, "yyyy-MM-dd HH:mm:ss")); + using (var uow = UnitOfWorkManager.Begin(true)) + { + await AuditLogRepository.InsertAsync(await Converter.ConvertAsync(auditInfo)); + await uow.CompleteAsync(); + } + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Entities/AuditLog.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Entities/AuditLogAggregateRoot.cs similarity index 100% rename from Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Entities/AuditLog.cs rename to Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Entities/AuditLogAggregateRoot.cs diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/EntityChangeWithUsername.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/EntityChangeWithUsername.cs new file mode 100644 index 00000000..f52b5319 --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/EntityChangeWithUsername.cs @@ -0,0 +1,10 @@ +using Yi.Framework.AuditLogging.Domain.Entities; + +namespace Yi.Framework.AuditLogging.Domain; + +public class EntityChangeWithUsername +{ + public EntityChangeEntity EntityChange { get; set; } + + public string UserName { get; set; } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/IAuditLogInfoToAuditLogConverter.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/IAuditLogInfoToAuditLogConverter.cs new file mode 100644 index 00000000..51e4ce95 --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/IAuditLogInfoToAuditLogConverter.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Volo.Abp.Auditing; +using Yi.Framework.AuditLogging.Domain.Entities; + +namespace Yi.Framework.AuditLogging.Domain; + +public interface IAuditLogInfoToAuditLogConverter +{ + Task ConvertAsync(AuditLogInfo auditLogInfo); +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Repositories/IAuditLogRepository.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Repositories/IAuditLogRepository.cs new file mode 100644 index 00000000..7396b51b --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Repositories/IAuditLogRepository.cs @@ -0,0 +1,19 @@ +using System.Net; +using Volo.Abp.Auditing; +using Yi.Framework.AuditLogging.Domain.Entities; +using Yi.Framework.SqlSugarCore.Abstractions; + +namespace Yi.Framework.AuditLogging.Domain.Repositories +{ + public interface IAuditLogRepository : ISqlSugarRepository + { + Task> GetAverageExecutionDurationPerDayAsync(DateTime startDate, DateTime endDate, CancellationToken cancellationToken = default); + Task GetCountAsync(DateTime? startTime = null, DateTime? endTime = null, string httpMethod = null, string url = null, Guid? userId = null, string userName = null, string applicationName = null, string clientIpAddress = null, string correlationId = null, int? maxExecutionDuration = null, int? minExecutionDuration = null, bool? hasException = null, HttpStatusCode? httpStatusCode = null, CancellationToken cancellationToken = default); + Task GetEntityChange(Guid entityChangeId, CancellationToken cancellationToken = default); + Task GetEntityChangeCountAsync(Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, EntityChangeType? changeType = null, string entityId = null, string entityTypeFullName = null, CancellationToken cancellationToken = default); + Task> GetEntityChangeListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, EntityChangeType? changeType = null, string entityId = null, string entityTypeFullName = null, bool includeDetails = false, CancellationToken cancellationToken = default); + Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName, CancellationToken cancellationToken = default); + Task GetEntityChangeWithUsernameAsync(Guid entityChangeId); + Task> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, DateTime? startTime = null, DateTime? endTime = null, string httpMethod = null, string url = null, Guid? userId = null, string userName = null, string applicationName = null, string clientIpAddress = null, string correlationId = null, int? maxExecutionDuration = null, int? minExecutionDuration = null, bool? hasException = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false); + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Yi.Framework.AuditLogging.Domain.csproj b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Yi.Framework.AuditLogging.Domain.csproj index 46b5eced..a9119698 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Yi.Framework.AuditLogging.Domain.csproj +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/Yi.Framework.AuditLogging.Domain.csproj @@ -12,6 +12,7 @@ + diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/YiFrameworkAuditLoggingDomainModule.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/YiFrameworkAuditLoggingDomainModule.cs index e5687bd5..96fb03e9 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/YiFrameworkAuditLoggingDomainModule.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/YiFrameworkAuditLoggingDomainModule.cs @@ -1,7 +1,13 @@ -namespace Yi.Framework.AuditLogging.Domain -{ - public class YiFrameworkAuditLoggingDomainModule - { +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; +namespace Yi.Framework.AuditLogging.Domain +{ + public class YiFrameworkAuditLoggingDomainModule:AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + + } } } diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs deleted file mode 100644 index 3d8e8c8c..00000000 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Volo.Abp.AuditLogging; - -namespace Yi.AuditLogging.SqlSugarCore.Entities -{ - public class AuditLogEntity : AuditLog - { - public AuditLogEntity() { } - } -} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Repositories/SqlSugarCoreAuditLogRepository.cs similarity index 56% rename from Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs rename to Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Repositories/SqlSugarCoreAuditLogRepository.cs index 0c2a4726..0e79c73c 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Repositories/SqlSugarCoreAuditLogRepository.cs @@ -1,21 +1,22 @@ using System.Linq.Dynamic.Core; -using System.Linq.Expressions; using System.Net; -using Mapster; using SqlSugar; using Volo.Abp.Auditing; -using Volo.Abp.AuditLogging; -using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; -using Volo.Abp.Domain.Repositories; -using Yi.AuditLogging.SqlSugarCore.Entities; +using Yi.Framework.AuditLogging.Domain; +using Yi.Framework.AuditLogging.Domain.Entities; +using Yi.Framework.AuditLogging.Domain.Repositories; +using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Repositories; -namespace Yi.AuditLogging.SqlSugarCore; +namespace Yi.Framework.AuditLogging.SqlSugarCore.Repositories; -public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository, IAuditLogRepository +public class SqlSugarCoreAuditLogRepository : SqlSugarRepository, IAuditLogRepository { - public virtual async Task> GetListAsync( + public SqlSugarCoreAuditLogRepository(ISugarDbContextProvider sugarDbContextProvider) : base(sugarDbContextProvider) + { + } + public virtual async Task> GetListAsync( string sorting = null, int maxResultCount = 50, int skipCount = 0, @@ -32,8 +33,7 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository>(); + return auditLogs; } public virtual async Task GetCountAsync( @@ -91,12 +91,12 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository> GetListQueryAsync( + protected virtual async Task> GetListQueryAsync( DateTime? startTime = null, DateTime? endTime = null, string httpMethod = null, @@ -113,7 +113,7 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository() + return _DbQueryable .WhereIF(startTime.HasValue, auditLog => auditLog.ExecutionTime >= startTime) .WhereIF(endTime.HasValue, auditLog => auditLog.ExecutionTime <= endTime) .WhereIF(hasException.HasValue && hasException.Value, auditLog => auditLog.Exceptions != null && auditLog.Exceptions != "") @@ -135,34 +135,35 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository() + var result = await _DbQueryable .Where(a => a.ExecutionTime < endDate.AddDays(1) && a.ExecutionTime > startDate) .OrderBy(t => t.ExecutionTime) .GroupBy(t => new { t.ExecutionTime.Date }) .Select(g => new { Day = SqlFunc.AggregateMin(g.ExecutionTime), avgExecutionTime = SqlFunc.AggregateAvg(g.ExecutionDuration) }) - .ToListAsync(cancellationToken); + .ToListAsync(); return result.ToDictionary(element => element.Day.ClearTime(), element => (double)element.avgExecutionTime); } - public virtual async Task GetEntityChange( + + public virtual async Task GetEntityChange( Guid entityChangeId, CancellationToken cancellationToken = default) { - var entityChange = await (await GetDbContextAsync()).Queryable() + var entityChange = await (await GetDbContextAsync()).Queryable() .Where(x => x.Id == entityChangeId) .OrderBy(x => x.Id) - .FirstAsync(cancellationToken); + .FirstAsync(); if (entityChange == null) { - throw new EntityNotFoundException(typeof(EntityChange)); + throw new EntityNotFoundException(typeof(EntityChangeEntity)); } return entityChange; } - public virtual async Task> GetEntityChangeListAsync( + public virtual async Task> GetEntityChangeListAsync( string sorting = null, int maxResultCount = 50, int skipCount = 0, @@ -177,8 +178,8 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository GetEntityChangeCountAsync( @@ -192,17 +193,16 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository GetEntityChangeWithUsernameAsync( - Guid entityChangeId, - CancellationToken cancellationToken = default) + Guid entityChangeId) { - var auditLog = await (await GetDbContextAsync()).Queryable() - .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(cancellationToken); + var auditLog = await _DbQueryable + .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(); return new EntityChangeWithUsername() { @@ -218,15 +218,14 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository() + var query = dbContext.Queryable() .Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); - return await query.LeftJoin((change, audit) => change.AuditLogId == audit.Id) - .Select((change, audit) => new EntityChangeWithUsername { EntityChange = change, UserName = audit.UserName }, true) - .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(cancellationToken); - + return await query.LeftJoin((x, audit) => x.AuditLogId == audit.Id) + .Select((x, audit) => new EntityChangeWithUsername { EntityChange = x, UserName = audit.UserName }) + .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(); } - protected virtual async Task> GetEntityChangeListQueryAsync( + protected virtual async Task> GetEntityChangeListQueryAsync( Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, @@ -236,7 +235,7 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository() + .Queryable() .WhereIF(auditLogId.HasValue, e => e.AuditLogId == auditLogId) .WhereIF(startTime.HasValue, e => e.ChangeTime >= startTime) .WhereIF(endTime.HasValue, e => e.ChangeTime <= endTime) @@ -244,106 +243,4 @@ public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository e.EntityId == entityId) .WhereIF(!string.IsNullOrWhiteSpace(entityTypeFullName), e => e.EntityTypeFullName.Contains(entityTypeFullName)); } - - Task> IAuditLogRepository.GetListAsync(string sorting, int maxResultCount, int skipCount, DateTime? startTime, DateTime? endTime, string httpMethod, string url, Guid? userId, string userName, string applicationName, string clientIpAddress, string correlationId, int? maxExecutionDuration, int? minExecutionDuration, bool? hasException, HttpStatusCode? httpStatusCode, bool includeDetails, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - - - public Task GetAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task DeleteDirectAsync(Expression> predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - IQueryable IReadOnlyRepository.WithDetails() - { - throw new NotImplementedException(); - } - - public IQueryable WithDetails(params Expression>[] propertySelectors) - { - throw new NotImplementedException(); - } - - Task> IReadOnlyRepository.WithDetailsAsync() - { - throw new NotImplementedException(); - } - - public Task> WithDetailsAsync(params Expression>[] propertySelectors) - { - throw new NotImplementedException(); - } - - Task> IReadOnlyRepository.GetQueryableAsync() - { - throw new NotImplementedException(); - } - - public Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task InsertAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task UpdateAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task DeleteAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - Task IReadOnlyBasicRepository.GetAsync(Guid id, bool includeDetails, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - Task IReadOnlyBasicRepository.FindAsync(Guid id, bool includeDetails, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - Task> IReadOnlyBasicRepository.GetListAsync(bool includeDetails, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - Task> IReadOnlyBasicRepository.GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } } diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Yi.Framework.AuditLogging.SqlSugarCore.csproj b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Yi.Framework.AuditLogging.SqlSugarCore.csproj index b27ef778..b7b0f931 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Yi.Framework.AuditLogging.SqlSugarCore.csproj +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/Yi.Framework.AuditLogging.SqlSugarCore.csproj @@ -6,13 +6,10 @@ enable - - - - + diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs deleted file mode 100644 index 5922f30b..00000000 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System.Reflection; -using SqlSugar; -using Volo.Abp.AuditLogging; -using Volo.Abp.DependencyInjection; -using Yi.Framework.SqlSugarCore; - -namespace Yi.AuditLogging.SqlSugarCore -{ - public class YiAuditLoggingDbContext : SqlSugarDbContext - { - public YiAuditLoggingDbContext(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider) - { - } - - protected override void EntityService(PropertyInfo property, EntityColumnInfo column) - { - base.EntityService(property, column); - column.DbTableName = AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogs"; - - column.IfTable() - - .UpdateProperty(x => x.Id, - x => - { - x.IsPrimarykey = true; - }) - - .UpdateProperty(x => x.ApplicationName, - x => - { - x.Length = AuditLogConsts.MaxApplicationNameLength; - x.DbColumnName = nameof(AuditLog.ApplicationName); - }); - - - // builder.Entity(b => - // { - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogs", AbpAuditLoggingDbProperties.DbSchema); - - // b.ConfigureByConvention(); - - // b.Property(x => x.ApplicationName).HasMaxLength(AuditLogConsts.MaxApplicationNameLength).HasColumnName(nameof(AuditLog.ApplicationName)); - // b.Property(x => x.ClientIpAddress).HasMaxLength(AuditLogConsts.MaxClientIpAddressLength).HasColumnName(nameof(AuditLog.ClientIpAddress)); - // b.Property(x => x.ClientName).HasMaxLength(AuditLogConsts.MaxClientNameLength).HasColumnName(nameof(AuditLog.ClientName)); - // b.Property(x => x.ClientId).HasMaxLength(AuditLogConsts.MaxClientIdLength).HasColumnName(nameof(AuditLog.ClientId)); - // b.Property(x => x.CorrelationId).HasMaxLength(AuditLogConsts.MaxCorrelationIdLength).HasColumnName(nameof(AuditLog.CorrelationId)); - // b.Property(x => x.BrowserInfo).HasMaxLength(AuditLogConsts.MaxBrowserInfoLength).HasColumnName(nameof(AuditLog.BrowserInfo)); - // b.Property(x => x.HttpMethod).HasMaxLength(AuditLogConsts.MaxHttpMethodLength).HasColumnName(nameof(AuditLog.HttpMethod)); - // b.Property(x => x.Url).HasMaxLength(AuditLogConsts.MaxUrlLength).HasColumnName(nameof(AuditLog.Url)); - // b.Property(x => x.HttpStatusCode).HasColumnName(nameof(AuditLog.HttpStatusCode)); - - // b.Property(x => x.Comments).HasMaxLength(AuditLogConsts.MaxCommentsLength).HasColumnName(nameof(AuditLog.Comments)); - // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLog.ExecutionDuration)); - // b.Property(x => x.ImpersonatorTenantId).HasColumnName(nameof(AuditLog.ImpersonatorTenantId)); - // b.Property(x => x.ImpersonatorUserId).HasColumnName(nameof(AuditLog.ImpersonatorUserId)); - // b.Property(x => x.ImpersonatorTenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.ImpersonatorTenantName)); - // b.Property(x => x.ImpersonatorUserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.ImpersonatorUserName)); - // b.Property(x => x.UserId).HasColumnName(nameof(AuditLog.UserId)); - // b.Property(x => x.UserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.UserName)); - // b.Property(x => x.TenantId).HasColumnName(nameof(AuditLog.TenantId)); - // b.Property(x => x.TenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.TenantName)); - - // b.HasMany(a => a.Actions).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); - // b.HasMany(a => a.EntityChanges).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); - - // b.HasIndex(x => new { x.TenantId, x.ExecutionTime }); - // b.HasIndex(x => new { x.TenantId, x.UserId, x.ExecutionTime }); - - // b.ApplyObjectExtensionMappings(); - // }); - - // builder.Entity(b => - // { - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogActions", AbpAuditLoggingDbProperties.DbSchema); - - // b.ConfigureByConvention(); - - // b.Property(x => x.AuditLogId).HasColumnName(nameof(AuditLogAction.AuditLogId)); - // b.Property(x => x.ServiceName).HasMaxLength(AuditLogActionConsts.MaxServiceNameLength).HasColumnName(nameof(AuditLogAction.ServiceName)); - // b.Property(x => x.MethodName).HasMaxLength(AuditLogActionConsts.MaxMethodNameLength).HasColumnName(nameof(AuditLogAction.MethodName)); - // b.Property(x => x.Parameters).HasMaxLength(AuditLogActionConsts.MaxParametersLength).HasColumnName(nameof(AuditLogAction.Parameters)); - // b.Property(x => x.ExecutionTime).HasColumnName(nameof(AuditLogAction.ExecutionTime)); - // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLogAction.ExecutionDuration)); - - // b.HasIndex(x => new { x.AuditLogId }); - // b.HasIndex(x => new { x.TenantId, x.ServiceName, x.MethodName, x.ExecutionTime }); - - // b.ApplyObjectExtensionMappings(); - // }); - - // builder.Entity(b => - // { - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityChanges", AbpAuditLoggingDbProperties.DbSchema); - - // b.ConfigureByConvention(); - - // b.Property(x => x.EntityTypeFullName).HasMaxLength(EntityChangeConsts.MaxEntityTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityChange.EntityTypeFullName)); - // b.Property(x => x.EntityId).HasMaxLength(EntityChangeConsts.MaxEntityIdLength).IsRequired().HasColumnName(nameof(EntityChange.EntityId)); - // b.Property(x => x.AuditLogId).IsRequired().HasColumnName(nameof(EntityChange.AuditLogId)); - // b.Property(x => x.ChangeTime).IsRequired().HasColumnName(nameof(EntityChange.ChangeTime)); - // b.Property(x => x.ChangeType).IsRequired().HasColumnName(nameof(EntityChange.ChangeType)); - // b.Property(x => x.TenantId).HasColumnName(nameof(EntityChange.TenantId)); - - // b.HasMany(a => a.PropertyChanges).WithOne().HasForeignKey(x => x.EntityChangeId); - - // b.HasIndex(x => new { x.AuditLogId }); - // b.HasIndex(x => new { x.TenantId, x.EntityTypeFullName, x.EntityId }); - - // b.ApplyObjectExtensionMappings(); - // }); - - // builder.Entity(b => - // { - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityPropertyChanges", AbpAuditLoggingDbProperties.DbSchema); - - // b.ConfigureByConvention(); - - // b.Property(x => x.NewValue).HasMaxLength(EntityPropertyChangeConsts.MaxNewValueLength).HasColumnName(nameof(EntityPropertyChange.NewValue)); - // b.Property(x => x.PropertyName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyName)); - // b.Property(x => x.PropertyTypeFullName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyTypeFullName)); - // b.Property(x => x.OriginalValue).HasMaxLength(EntityPropertyChangeConsts.MaxOriginalValueLength).HasColumnName(nameof(EntityPropertyChange.OriginalValue)); - - // b.HasIndex(x => new { x.EntityChangeId }); - - // b.ApplyObjectExtensionMappings(); - // }); - //} - } - } -} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiFrameworkAuditLoggingSqlSugarCoreModule.cs b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiFrameworkAuditLoggingSqlSugarCoreModule.cs index 7fc06665..f16d83d9 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiFrameworkAuditLoggingSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.SqlSugarCore/YiFrameworkAuditLoggingSqlSugarCoreModule.cs @@ -1,19 +1,17 @@ using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Volo.Abp.AuditLogging; using Volo.Abp.Modularity; +using Yi.Framework.AuditLogging.Domain.Repositories; +using Yi.Framework.AuditLogging.SqlSugarCore.Repositories; using Yi.Framework.SqlSugarCore; namespace Yi.AuditLogging.SqlSugarCore { - [DependsOn(typeof(AbpAuditLoggingDomainModule))] [DependsOn(typeof(YiFrameworkSqlSugarCoreModule))] public class YiFrameworkAuditLoggingSqlSugarCoreModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) - { - context.Services.Replace(new ServiceDescriptor(typeof(IAuditLogRepository), typeof(SqlSugarCoreAuditLogRepository), lifetime: ServiceLifetime.Transient)); - context.Services.TryAddYiDbContext(); + { + context.Services.AddTransient(); } }