VzAuditingHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. using System.Transactions;
  4. using Abp.Auditing;
  5. using Abp.Dependency;
  6. using Abp.Domain.Uow;
  7. using Abp.Runtime.Session;
  8. using Abp.Timing;
  9. using Castle.Core.Logging;
  10. using VberZero.Tools;
  11. using ILogger = Castle.Core.Logging.ILogger;
  12. namespace VberZero.Auditing;
  13. public class VzAuditingHelper : IAuditingHelper, ITransientDependency
  14. {
  15. private readonly IAuditInfoProvider _auditInfoProvider;
  16. private readonly IAuditingConfiguration _configuration;
  17. private readonly IUnitOfWorkManager _unitOfWorkManager;
  18. private readonly IAuditSerializer _auditSerializer;
  19. public ILogger Logger { get; set; }
  20. public IAbpSession AbpSession { get; set; }
  21. public IAuditingStore AuditingStore { get; }
  22. public VzAuditingHelper(
  23. IAuditInfoProvider auditInfoProvider,
  24. IAuditingConfiguration configuration,
  25. IUnitOfWorkManager unitOfWorkManager,
  26. IAuditSerializer auditSerializer, IAuditingStore auditingStore)
  27. {
  28. _auditInfoProvider = auditInfoProvider;
  29. _configuration = configuration;
  30. _unitOfWorkManager = unitOfWorkManager;
  31. _auditSerializer = auditSerializer;
  32. AuditingStore = auditingStore;
  33. AbpSession = NullAbpSession.Instance;
  34. Logger = NullLogger.Instance;
  35. }
  36. public bool ShouldSaveAudit(MethodInfo methodInfo, bool defaultValue = false)
  37. {
  38. if (!_configuration.IsEnabled)
  39. return false;
  40. if (!_configuration.IsEnabledForAnonymousUsers)
  41. {
  42. IAbpSession abpSession = AbpSession;
  43. if ((abpSession != null ? (!abpSession.UserId.HasValue ? 1 : 0) : 1) != 0)
  44. return false;
  45. }
  46. if (methodInfo == null || !methodInfo.IsPublic)
  47. return false;
  48. if (methodInfo.IsDefined(typeof(AuditedAttribute), true))
  49. return true;
  50. if (methodInfo.IsDefined(typeof(DisableAuditingAttribute), true))
  51. return false;
  52. Type classType = methodInfo.DeclaringType;
  53. if (classType != null)
  54. {
  55. if (classType.GetTypeInfo().IsDefined(typeof(AuditedAttribute), true))
  56. return true;
  57. if (classType.GetTypeInfo().IsDefined(typeof(DisableAuditingAttribute), true))
  58. return false;
  59. if (_configuration.Selectors.Any(selector => selector.Predicate(classType)))
  60. return true;
  61. }
  62. return defaultValue;
  63. }
  64. public AuditInfo CreateAuditInfo(Type type, MethodInfo method, object[] arguments) => CreateAuditInfo(type, method, CreateArgumentsDictionary(method, arguments));
  65. public AuditInfo CreateAuditInfo(Type type, MethodInfo method, IDictionary<string, object> arguments)
  66. {
  67. AuditInfo auditInfo = new AuditInfo()
  68. {
  69. TenantId = AbpSession.TenantId,
  70. UserId = AbpSession.UserId,
  71. ImpersonatorUserId = AbpSession.ImpersonatorUserId,
  72. ImpersonatorTenantId = AbpSession.ImpersonatorTenantId,
  73. ServiceName = type != null ? type.FullName : "",
  74. MethodName = method.Name,
  75. Parameters = ConvertArgumentsToJson(arguments),
  76. ExecutionTime = Clock.Now
  77. };
  78. try
  79. {
  80. _auditInfoProvider.Fill(auditInfo);
  81. }
  82. catch (Exception ex)
  83. {
  84. Logger.Warn(ex.ToString(), ex);
  85. }
  86. return auditInfo;
  87. }
  88. public void Save(AuditInfo auditInfo)
  89. {
  90. using (IUnitOfWorkCompleteHandle workCompleteHandle = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
  91. {
  92. AuditingStore.Save(auditInfo);
  93. workCompleteHandle.Complete();
  94. }
  95. }
  96. public async Task SaveAsync(AuditInfo auditInfo)
  97. {
  98. using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
  99. {
  100. ConfiguredTaskAwaitable configuredTaskAwaitable = AuditingStore.SaveAsync(auditInfo).ConfigureAwait(false);
  101. await configuredTaskAwaitable;
  102. configuredTaskAwaitable = uow.CompleteAsync().ConfigureAwait(false);
  103. await configuredTaskAwaitable;
  104. }
  105. }
  106. private string ConvertArgumentsToJson(IDictionary<string, object> arguments)
  107. {
  108. try
  109. {
  110. if (arguments.IsNullOrEmpty())
  111. return "{}";
  112. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  113. foreach (KeyValuePair<string, object> keyValuePair in arguments)
  114. {
  115. KeyValuePair<string, object> argument = keyValuePair;
  116. dictionary[argument.Key] = argument.Value == null || !_configuration.IgnoredTypes.Any((t => t.IsInstanceOfType(argument.Value))) ? argument.Value : null;
  117. }
  118. return _auditSerializer.Serialize(dictionary);
  119. }
  120. catch (Exception ex)
  121. {
  122. Logger.Warn(ex.ToString(), ex);
  123. return "{}";
  124. }
  125. }
  126. private static Dictionary<string, object> CreateArgumentsDictionary(
  127. MethodInfo method,
  128. object[] arguments)
  129. {
  130. ParameterInfo[] parameters = method.GetParameters();
  131. Dictionary<string, object> argumentsDictionary = new Dictionary<string, object>();
  132. for (int index = 0; index < parameters.Length; ++index)
  133. {
  134. var name = parameters[index].Name;
  135. if (name != null)
  136. argumentsDictionary[name] = arguments[index];
  137. }
  138. return argumentsDictionary;
  139. }
  140. }