ElasticSearchLoggingWriter.cs 4.4 KB

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