Ver código fonte

删除动态代理AOP

qinchaoyue 3 meses atrás
pai
commit
5055882b60

+ 0 - 4
src/Hotline.Api/StartupExtensions.cs

@@ -40,7 +40,6 @@ using Microsoft.AspNetCore.ResponseCompression;
 using Hotline.WeChat;
 using Hotline.Snapshot.Interfaces;
 using TianQue.Sdk;
-using Hotline.Settings.SystemLogDomain;
 using Hotline.Application.Snapshot;
 using Hotline.Orders;
 using XF.Domain.Repository.Events;
@@ -136,9 +135,6 @@ internal static class StartupExtensions
         //mediatr
         services.RegisterMediatR(appConfiguration);
 
-        services.AddScoped<LoggingInterceptor>();
-        services.AddScoped<AsyncLoggingInterceptor>();
-
         //app scope
         switch (appConfiguration.AppScope)
         {

+ 0 - 5
src/Hotline.Application.Tests/Startup.cs

@@ -63,7 +63,6 @@ using Hotline.Snapshot.Interfaces;
 using TianQue.Sdk;
 using Hotline.Snapshot.Notifications;
 using Hotline.File;
-using Hotline.Settings.SystemLogDomain;
 using Hotline.Application.Tests.Mock.Interfaces;
 using Hotline.Orders.DatabaseEventHandler;
 using Hotline.Orders;
@@ -194,11 +193,7 @@ public class Startup
             services.AddScoped<IFileDomainService, FileDomainService>();
             services.AddXingTangDb(callCenterConfiguration.XingTang);
             services.AddScoped<IGuiderSystemService, TiqnQueService>();
-            services.AddScoped<LoggingInterceptor>();
-            services.AddScoped<AsyncLoggingInterceptor>();
 
-            // application services
-            services.AddProxiedScoped<ISnapshotApplication, DefaultSnapshotApplication>();
             //ServiceLocator.Instance = services.BuildServiceProvider();
         }
 

+ 1 - 2
src/Hotline.Application/Snapshot/ISnapshotApplication.cs

@@ -1,5 +1,4 @@
-using Hotline.Settings.SystemLogDomain;
-using Hotline.Share.Dtos;
+using Hotline.Share.Dtos;
 using Hotline.Share.Dtos.Article;
 using Hotline.Share.Dtos.Snapshot;
 using Hotline.Snapshot;

+ 4 - 0
src/Hotline.Application/Snapshot/IndustryApplication.cs

@@ -13,6 +13,7 @@ using Mapster;
 using SqlSugar;
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using System.Threading;
@@ -71,6 +72,7 @@ public class IndustryApplication : IIndustryApplication, IScopeDependency
         return id;
     }
 
+    [Description("行业集合")]
     public ISugarQueryable<IndustryItemsOutDto> GetIndustres(IndustryListInDto dto)
     {
         var query = _industryRepository.Queryable()
@@ -122,6 +124,7 @@ public class IndustryApplication : IIndustryApplication, IScopeDependency
     }
 
     #region 行业线索
+    [Description("行业线索")]
     public ISugarQueryable<IndustryCaseItemOutDto> GetIndustryCaseItems(IndustryCaseItemInDto dto)
     {
         var query = _industryCaseRepository.Queryable()
@@ -179,6 +182,7 @@ public class IndustryApplication : IIndustryApplication, IScopeDependency
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
+    [Description("行业模板")]
     public ISugarQueryable<SnapshotSMSTemplateItemsOutDto> GetSMSTemplates(SnapshotSMSTemplateItemsInDto dto)
     {
         var query = _snapshotSMSTemplateRepository.Queryable()

+ 5 - 0
src/Hotline.Application/Snapshot/RedPackApplication.cs

@@ -203,6 +203,7 @@ public class RedPackApplication : IRedPackApplication, IScopeDependency
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
+    [Description("市民红包审批")]
     public ISugarQueryable<SnapshotOrderAuditItemsOutDto> GetRedPackAuditItemsAsync(SnapshotOrderAuditItemsInDto dto)
     {
         ERedPackAuditStatus? status = null;
@@ -280,6 +281,7 @@ public class RedPackApplication : IRedPackApplication, IScopeDependency
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
+    [Description("网格员红包审批")]
     public ISugarQueryable<SnapshotOrderGuiderAuditItemsOutDto> GetRedPackGuiderAuditItemsAsync(SnapshotOrderGuiderAuditItemsInDto dto)
     {
         var areaCode = _sessionContext.OrgAreaCode;
@@ -427,6 +429,7 @@ public class RedPackApplication : IRedPackApplication, IScopeDependency
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
+    [Description("市民红包发放记录")]
     public ISugarQueryable<SnapshotRedPackRecordItemsOutDto> GetRedPackRecordItemsAsync(SnapshotRedPackRecordItemsInDto dto)
     {
         var query = _redPackRecordRepository.Queryable()
@@ -463,6 +466,7 @@ public class RedPackApplication : IRedPackApplication, IScopeDependency
         return order;
     }
 
+    [Description("网格员红包发放记录")]
     public ISugarQueryable<SnapshotRedPackRecordItemsGuiderOutDto> GetRedPackRecordGuiderItemsAsync(SnapshotRedPackRecordItemsGuiderInDto dto)
     {
         var query = _redPackRecordRepository.Queryable()
@@ -483,6 +487,7 @@ public class RedPackApplication : IRedPackApplication, IScopeDependency
         return query;
     }
 
+    [Description("红包发放明细")]
     public ISugarQueryable<SnapshotRedPackRecordSendOutDto> GetRedPackRecordDetailAsync(SnapshotRedPackRecordSendInDto dto)
     {
         var query = _redPackRecordRepository.Queryable()

+ 0 - 3
src/Hotline.Application/Snapshot/SnapshotApplicationBase.cs

@@ -34,7 +34,6 @@ using Hotline.Share.Mq;
 using Hotline.Snapshot.Notifications;
 using Hotline.EventBus;
 using Hotline.Quality.Notifications;
-using Hotline.Settings.SystemLogDomain;
 using XF.Utility.EnumExtensions;
 
 namespace Hotline.Application.Snapshot;
@@ -625,7 +624,6 @@ public abstract class SnapshotApplicationBase
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
-    [LogToData("ReplyCode")]
     public async Task SaveGuiderSystemReplyAsync(GuiderSystemInDto dto, CancellationToken token)
     {
         var orderSnapshot = await _orderSnapshotRepository.GetByNetworkENumberAsync(dto.AppealNumber)
@@ -824,7 +822,6 @@ public abstract class SnapshotApplicationBase
     /// <param name="id"></param>
     /// <param name="cancellationToken"></param>
     /// <returns></returns>
-    [LogToData]
     public async Task<string> AddRedPardAsync(string orderId, CancellationToken cancellationToken)
     {
         var order = await _orderRepository.Queryable()

+ 0 - 2
src/Hotline/Hotline.csproj

@@ -8,8 +8,6 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Castle.Core" Version="5.1.1" />
-    <PackageReference Include="Castle.Core.AsyncInterceptor" Version="2.1.0" />
     <PackageReference Include="Mapster" Version="7.4.0" />
     <PackageReference Include="MediatR" Version="12.4.1" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />

+ 0 - 134
src/Hotline/Settings/SystemLogDomain/AsyncLoggingInterceptor.cs

@@ -1,134 +0,0 @@
-using Castle.DynamicProxy;
-using DotNetCore.CAP;
-using Hotline.Share.Mq;
-using Hotline.Share.Tools;
-using Microsoft.Extensions.Logging;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-using XF.Domain.Dependency;
-
-namespace Hotline.Settings.SystemLogDomain;
-public class AsyncLoggingInterceptor : AsyncInterceptorBase
-{
-
-    private readonly ICapPublisher _capPublisher;
-    private readonly ILogger<AsyncLoggingInterceptor> _logger;
-
-    public AsyncLoggingInterceptor(ICapPublisher capPublisher, ILogger<AsyncLoggingInterceptor> logger)
-    {
-        _capPublisher = capPublisher;
-        _logger = logger;
-    }
-
-    protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task> proceed)
-    {
-        var logAttribute = GetLogAttribute(invocation);
-        var logEntity = GetSystemLog(invocation, logAttribute);
-        try
-        {
-            // 执行原始方法
-            await proceed(invocation, proceedInfo);
-            PublishLog(logEntity, null);
-        }
-        catch (Exception ex)
-        {
-            throw;
-        }
-    }
-
-    protected override async Task<TResult> InterceptAsync<TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task<TResult>> proceed)
-    {
-        var logEntity = GetSystemLog(invocation, GetLogAttribute(invocation));
-        try
-        {
-            // 执行原始方法
-            TResult result = await proceed(invocation, proceedInfo);
-            PublishLog(logEntity, result);
-            return result;
-        }
-        catch (Exception ex)
-        {
-            throw;
-        }
-    }
-
-    private LogToDataAttribute? GetLogAttribute(IInvocation invocation)
-    {
-        var methodInfo = invocation.MethodInvocationTarget ?? invocation.Method;
-        return methodInfo.GetCustomAttributes(typeof(LogToDataAttribute), true)
-                                     .FirstOrDefault() as LogToDataAttribute;
-    }
-
-    private SystemLog GetSystemLog(IInvocation invocation, LogToDataAttribute logAttribute)
-    {
-        if (logAttribute == null) return null;
-        var entity = new SystemLog
-        {
-            Name = invocation.Method.Name,
-            StartTime = DateTime.Now
-        };
-        for (int i = 0;i < invocation.Arguments.Length;i++)
-        {
-            var argument = invocation.Arguments[i];
-            if (argument == null || argument is CancellationToken) continue;
-            entity.ExecuteParam = argument;
-            Type argType = argument.GetType();
-            if (argument is string)
-            {
-                entity.ExternalId = argument.ToString();
-                continue;
-            }
-
-            var properties = argType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
-            foreach (var property in properties)
-            {
-                var value = property.GetValue(argument);
-                if (value != null && property.Name.ToUpper() == logAttribute.ExternalIdName.ToUpper())
-                {
-                    entity.ExternalId = value.ToString();
-                    break;
-                }
-            }
-        }
-        return entity;
-    }
-
-    private void PublishLog(SystemLog logEntity, object? result)
-    {
-        try
-        {
-            if (logEntity != null)
-            {
-                logEntity.EndTime = DateTime.Now;
-                logEntity.ExecuteResult = result;
-                _capPublisher.Publish(EventNames.LoggingInterceptor, logEntity);
-            }
-
-        }
-        catch (Exception e)
-        {
-            _logger.LogError(e, "PublishLog Error");
-        }
-    }
-}
-
-public class LoggingInterceptorHandler : ICapSubscribe, ITransientDependency
-{
-    private readonly ISystemLogRepository _systemLogRepository;
-
-    public LoggingInterceptorHandler(ISystemLogRepository systemLogRepository)
-    {
-        _systemLogRepository = systemLogRepository;
-    }
-
-    [CapSubscribe(EventNames.LoggingInterceptor)]
-    public async Task Handle(SystemLog logEntity)
-    {
-        logEntity.Interval = (int)(logEntity.EndTime.Value - logEntity.StartTime.Value).TotalSeconds;
-        await _systemLogRepository.AddAsync(logEntity);
-    }
-}

+ 0 - 17
src/Hotline/Settings/SystemLogDomain/LogToDataAttribute.cs

@@ -1,17 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Hotline.Settings.SystemLogDomain;
-[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
-public class LogToDataAttribute : Attribute
-{
-    public string ExternalIdName { get; set; }
-
-    public LogToDataAttribute(string externalIdName = "Id")
-    {
-        ExternalIdName = externalIdName;
-    }
-}

+ 0 - 64
src/Hotline/Settings/SystemLogDomain/LoggingInterceptor.cs

@@ -1,64 +0,0 @@
-using Castle.DynamicProxy;
-using DotNetCore.CAP;
-using Hotline.Share.Mq;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Hotline.Settings.SystemLogDomain;
-public class LoggingInterceptor : IInterceptor
-{
-    private readonly ICapPublisher _cap;
-
-    public LoggingInterceptor(ICapPublisher cap)
-    {
-        _cap = cap;
-    }
-
-    public void Intercept(IInvocation invocation)
-    {
-        // 检查实现类的方法是否有 LogToDatabaseAttribute
-        var methodInfo = invocation.MethodInvocationTarget ?? invocation.Method;
-        var logAttribute = methodInfo.GetCustomAttributes(typeof(LogToDataAttribute), true)
-                                     .FirstOrDefault() as LogToDataAttribute;
-
-        SystemLog entity = null;
-        if (logAttribute != null)
-        {
-            entity = new SystemLog
-            {
-                Name = invocation.Method.Name,
-            };
-            // 遍历所有参数
-            for (int i = 0;i < invocation.Arguments.Length;i++)
-            {
-                var argument = invocation.Arguments[i];
-                if (argument == null) continue;
-                Type argType = argument.GetType();
-
-                // 遍历参数的所有属性及其值
-                var properties = argType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
-                foreach (var property in properties)
-                {
-                    var value = property.GetValue(argument);
-                    if (value != null && property.Name.ToUpper() == logAttribute.ExternalIdName.ToUpper())
-                    {
-                        entity.ExternalId = value.ToString();
-                        break;
-                    }
-                }
-            }
-        }
-
-        // 执行实际方法
-        invocation.Proceed();
-
-        if (logAttribute != null && entity != null)
-        {
-            _cap.Publish(EventNames.LoggingInterceptor, entity);
-        }
-    }
-}

+ 0 - 42
src/Hotline/Settings/SystemLogDomain/SystemLogServiceCollectionExtensions.cs

@@ -1,42 +0,0 @@
-using Castle.DynamicProxy;
-using Microsoft.Extensions.DependencyInjection;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Hotline.Settings.SystemLogDomain;
-public static class SystemLogServiceCollectionExtensions
-{
-    public static IServiceCollection AddProxiedScoped<TInterface, TImplementation>(this IServiceCollection services)
-        where TInterface : class
-        where TImplementation : class, TInterface
-    {
-        services.AddScoped<TImplementation>();
-        services.AddScoped<TInterface>(provider =>
-        {
-            var proxyGenerator = new ProxyGenerator();
-            var target = provider.GetRequiredService<TImplementation>();
-            var interceptor = provider.GetRequiredService<AsyncLoggingInterceptor>();
-            return proxyGenerator.CreateInterfaceProxyWithTarget<TInterface>(target, interceptor);
-        });
-
-        return services;
-    }
-
-    public static IServiceCollection AddProxiedScoped(this IServiceCollection services, Type serviceType, Type implementationType)
-    {
-        services.AddScoped(implementationType);
-        services.AddScoped(serviceType, provider =>
-        {
-            var proxyGenerator = new ProxyGenerator();
-            var target = provider.GetRequiredService(implementationType);
-            var interceptor = provider.GetRequiredService<AsyncLoggingInterceptor>();
-
-            return proxyGenerator.CreateInterfaceProxyWithTarget(serviceType, target, interceptor);
-        });
-
-        return services;
-    }
-}