Blame view

Yi.Abp.Net8/module/audit-logging/Yi.Framework.AuditLogging.Domain/AuditLogInfoToAuditLogConverter.cs 4.14 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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<AbpExceptionHandlingOptions> exceptionHandlingOptions)
      {
          GuidGenerator = guidGenerator;
          ExceptionToErrorInfoConverter = exceptionToErrorInfoConverter;
          JsonSerializer = jsonSerializer;
          ExceptionHandlingOptions = exceptionHandlingOptions.Value;
      }
  
      public virtual Task<AuditLogAggregateRoot> 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<EntityChangeEntity>();
  
          var actions = auditLogInfo
                            .Actions?
                            .Select(auditLogActionInfo => new AuditLogActionEntity(GuidGenerator.Create(), auditLogId, auditLogActionInfo, tenantId: auditLogInfo.TenantId))
                            .ToList()
                        ?? new List<AuditLogActionEntity>();
  
          var remoteServiceErrorInfos = auditLogInfo.Exceptions?.Select(exception => ExceptionToErrorInfoConverter.Convert(exception, options =>
                                            {
                                                options.SendExceptionsDetailsToClients = ExceptionHandlingOptions.SendExceptionsDetailsToClients;
                                                options.SendStackTraceToClients = ExceptionHandlingOptions.SendStackTraceToClients;
                                            }))
                                        ?? new List<RemoteServiceErrorInfo>();
  
          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);
      }
  }