ElasticSearchLoggingWriter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. using Elastic.Clients.Elasticsearch;
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// ES日志写入器
  12. /// </summary>
  13. public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable
  14. {
  15. private readonly IServiceScope _serviceScope;
  16. private readonly ElasticsearchClient _esClient;
  17. private readonly SysConfigService _sysConfigService;
  18. public ElasticSearchLoggingWriter(IServiceScopeFactory scopeFactory)
  19. {
  20. _serviceScope = scopeFactory.CreateScope();
  21. _esClient = _serviceScope.ServiceProvider.GetRequiredService<ElasticsearchClient>();
  22. _sysConfigService = _serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
  23. }
  24. public async Task WriteAsync(LogMessage logMsg, bool flush)
  25. {
  26. // 是否启用操作日志
  27. var sysOpLogEnabled = await _sysConfigService.GetConfigValue<bool>(CommonConst.SysOpLog);
  28. if (!sysOpLogEnabled) return;
  29. var jsonStr = logMsg.Context.Get("loggingMonitor").ToString();
  30. var loggingMonitor = JSON.Deserialize<dynamic>(jsonStr);
  31. // 不记录登录退出日志
  32. if (loggingMonitor.actionName == "userInfo" || loggingMonitor.actionName == "logout")
  33. return;
  34. // 获取当前操作者
  35. string account = "", realName = "", userId = "", tenantId = "";
  36. if (loggingMonitor.authorizationClaims != null)
  37. {
  38. foreach (var item in loggingMonitor.authorizationClaims)
  39. {
  40. if (item.type == ClaimConst.Account)
  41. account = item.value;
  42. if (item.type == ClaimConst.RealName)
  43. realName = item.value;
  44. if (item.type == ClaimConst.TenantId)
  45. tenantId = item.value;
  46. if (item.type == ClaimConst.UserId)
  47. userId = item.value;
  48. }
  49. }
  50. string remoteIPv4 = loggingMonitor.remoteIPv4;
  51. (string ipLocation, double? longitude, double? latitude) = DatabaseLoggingWriter.GetIpAddress(remoteIPv4);
  52. var sysLogOp = new SysLogOp
  53. {
  54. Id = DateTime.Now.Ticks,
  55. ControllerName = loggingMonitor.controllerName,
  56. ActionName = loggingMonitor.actionTypeName,
  57. DisplayTitle = loggingMonitor.displayTitle,
  58. Status = loggingMonitor.returnInformation.httpStatusCode,
  59. RemoteIp = remoteIPv4,
  60. Location = ipLocation,
  61. Longitude = longitude,
  62. Latitude = latitude,
  63. Browser = loggingMonitor.userAgent,
  64. Os = loggingMonitor.osDescription + " " + loggingMonitor.osArchitecture,
  65. Elapsed = loggingMonitor.timeOperationElapsedMilliseconds,
  66. LogDateTime = logMsg.LogDateTime,
  67. Account = account,
  68. RealName = realName,
  69. HttpMethod = loggingMonitor.httpMethod,
  70. RequestUrl = loggingMonitor.requestUrl,
  71. RequestParam = (loggingMonitor.parameters == null || loggingMonitor.parameters.Count == 0) ? null : JSON.Serialize(loggingMonitor.parameters[0].value),
  72. ReturnResult = JSON.Serialize(loggingMonitor.returnInformation),
  73. EventId = logMsg.EventId.Id,
  74. ThreadId = logMsg.ThreadId,
  75. TraceId = logMsg.TraceId,
  76. Exception = (loggingMonitor.exception == null) ? null : JSON.Serialize(loggingMonitor.exception),
  77. Message = logMsg.Message,
  78. CreateUserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
  79. TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : long.Parse(tenantId)
  80. };
  81. await _esClient.IndexAsync(sysLogOp);
  82. }
  83. /// <summary>
  84. /// 释放服务作用域
  85. /// </summary>
  86. public void Dispose()
  87. {
  88. _serviceScope.Dispose();
  89. }
  90. }