From 0f60ab870e721aec9ec41a47fa2ea3940d12c21e Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 11 Jun 2022 11:42:08 +0800 Subject: [PATCH] Write audit logs using the ES bulk interface --- .../ElasticsearchAuditLogManager.cs | 21 ++++++++++++------- .../Abp/AuditLogging/AuditLogAction.cs | 3 ++- .../SerilogElasticsearchLoggingManager.cs | 6 ++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging.Elasticsearch/LINGYUN/Abp/AuditLogging/Elasticsearch/ElasticsearchAuditLogManager.cs b/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging.Elasticsearch/LINGYUN/Abp/AuditLogging/Elasticsearch/ElasticsearchAuditLogManager.cs index f5e30aca0..2c08377bf 100644 --- a/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging.Elasticsearch/LINGYUN/Abp/AuditLogging/Elasticsearch/ElasticsearchAuditLogManager.cs +++ b/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging.Elasticsearch/LINGYUN/Abp/AuditLogging/Elasticsearch/ElasticsearchAuditLogManager.cs @@ -213,13 +213,20 @@ namespace LINGYUN.Abp.AuditLogging.Elasticsearch var auditLog = await _converter.ConvertAsync(auditLogInfo); - var response = await client.IndexAsync( - auditLog, - (x) => x.Index(CreateIndex()) - .Id(auditLog.Id), - cancellationToken); - - return response.Id; + //var response = await client.IndexAsync( + // auditLog, + // (x) => x.Index(CreateIndex()) + // .Id(auditLog.Id), + // cancellationToken); + + // 使用 Bulk 命令传输可能存在参数庞大的日志结构 + var response = await client.BulkAsync( + dsl => dsl.Index(CreateIndex()) + .Create(ct => + ct.Id(auditLog.Id) + .Document(auditLog))); + + return response.Items?.FirstOrDefault()?.Id; } protected virtual List, QueryContainer>> BuildQueryDescriptor( diff --git a/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging/LINGYUN/Abp/AuditLogging/AuditLogAction.cs b/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging/LINGYUN/Abp/AuditLogging/AuditLogAction.cs index 56f8b07e1..1f3811b2e 100644 --- a/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging/LINGYUN/Abp/AuditLogging/AuditLogAction.cs +++ b/aspnet-core/modules/auditing/LINGYUN.Abp.AuditLogging/LINGYUN/Abp/AuditLogging/AuditLogAction.cs @@ -41,7 +41,8 @@ namespace LINGYUN.Abp.AuditLogging ExtraProperties = new ExtraPropertyDictionary(actionInfo.ExtraProperties); ServiceName = actionInfo.ServiceName; MethodName = actionInfo.MethodName; - Parameters = actionInfo.Parameters.Length > 2000 ? "" : actionInfo.Parameters; + Parameters = actionInfo.Parameters; + // Parameters = actionInfo.Parameters.Length > 2000 ? "" : actionInfo.Parameters; } } } diff --git a/aspnet-core/modules/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs b/aspnet-core/modules/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs index 44fad6311..c68636d28 100644 --- a/aspnet-core/modules/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs +++ b/aspnet-core/modules/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs @@ -90,7 +90,8 @@ namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch (s) => s.Term( (t) => t.Field(GetField(nameof(SerilogInfo.Fields.UniqueId))).Value(id)), (s) => s.Term( - (t) => t.Field(GetField(nameof(SerilogInfo.Fields.TenantId))).Value(_currentTenant.GetId()))))), + (t) => t.Field(GetField(nameof(SerilogInfo.Fields.TenantId))).Value(_currentTenant.GetId()))))) + .Size(1), cancellationToken); } else @@ -117,7 +118,8 @@ namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch (q) => q.Bool( (b) => b.Must( (s) => s.Term( - (t) => t.Field(GetField(nameof(SerilogInfo.Fields.UniqueId))).Value(id))))), + (t) => t.Field(GetField(nameof(SerilogInfo.Fields.UniqueId))).Value(id))))) + .Size(1), cancellationToken); }