|
@@ -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);
|
|
|
- }
|
|
|
-}
|