Browse Source

fix(logging): Fix the ES query for logging

- 系统日志ES集成客户端库查询语法变更
pull/1416/head
colin 2 months ago
parent
commit
79aa4d1b88
  1. 111
      aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs
  2. 18
      aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogException.cs
  3. 33
      aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogField.cs
  4. 11
      aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogInfo.cs

111
aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogElasticsearchLoggingManager.cs

@ -1,10 +1,11 @@
using LINGYUN.Abp.Elasticsearch;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.QueryDsl;
using LINGYUN.Abp.Elasticsearch;
using LINGYUN.Abp.Serilog.Enrichers.Application;
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Nest;
using Serilog.Events;
using System;
using System.Collections.Generic;
@ -60,7 +61,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
{
var client = _clientFactory.Create();
ISearchResponse<SerilogInfo> response;
SearchResponse<SerilogInfo> response;
if (_currentTenant.IsAvailable)
{
@ -88,14 +89,14 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
*/
response = await client.SearchAsync<SerilogInfo>(
dsl =>
dsl.Index(CreateIndex())
dsl.Indices(CreateIndex())
.Query(
(q) => q.Bool(
(b) => b.Must(
(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().ToString())))))
.Size(1),
cancellationToken);
}
@ -118,7 +119,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
*/
response = await client.SearchAsync<SerilogInfo>(
dsl =>
dsl.Index(CreateIndex())
dsl.Indices(CreateIndex())
.Query(
(q) => q.Bool(
(b) => b.Must(
@ -134,7 +135,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
public async virtual Task<long> GetCountAsync(
DateTime? startTime = null,
DateTime? endTime = null,
Microsoft.Extensions.Logging.LogLevel? level = null,
LogLevel? level = null,
string machineName = null,
string environment = null,
string application = null,
@ -165,7 +166,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
hasException);
var response = await client.CountAsync<SerilogInfo>((dsl) =>
dsl.Index(CreateIndex())
dsl.Indices(CreateIndex())
.Query(log => log.Bool(b => b.Must(querys.ToArray()))),
cancellationToken);
@ -200,7 +201,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
int skipCount = 0,
DateTime? startTime = null,
DateTime? endTime = null,
Microsoft.Extensions.Logging.LogLevel? level = null,
LogLevel? level = null,
string machineName = null,
string environment = null,
string application = null,
@ -217,7 +218,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
var client = _clientFactory.Create();
var sortOrder = !sorting.IsNullOrWhiteSpace() && sorting.EndsWith("asc", StringComparison.InvariantCultureIgnoreCase)
? SortOrder.Ascending : SortOrder.Descending;
? SortOrder.Asc : SortOrder.Desc;
sorting = !sorting.IsNullOrWhiteSpace()
? sorting.Split()[0]
: nameof(SerilogInfo.TimeStamp);
@ -237,24 +238,12 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
threadId,
hasException);
SourceFilterDescriptor<SerilogInfo> SourceFilter(SourceFilterDescriptor<SerilogInfo> selector)
{
selector.IncludeAll();
if (!includeDetails)
{
selector.Excludes(field =>
field.Field("exceptions"));
}
return selector;
}
var response = await client.SearchAsync<SerilogInfo>((dsl) =>
dsl.Index(CreateIndex())
dsl.Indices(CreateIndex())
.Query(log =>
log.Bool(b =>
b.Must(querys.ToArray())))
.Source(SourceFilter)
.SourceExcludes(se => se.Exceptions)
.Sort(log => log.Field(GetField(sorting), sortOrder))
.From(skipCount)
.Size(maxResultCount),
@ -263,10 +252,10 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
return _objectMapper.Map<List<SerilogInfo>, List<LogInfo>>(response.Documents.ToList());
}
protected virtual List<Func<QueryContainerDescriptor<SerilogInfo>, QueryContainer>> BuildQueryDescriptor(
protected virtual List<Query> BuildQueryDescriptor(
DateTime? startTime = null,
DateTime? endTime = null,
Microsoft.Extensions.Logging.LogLevel? level = null,
LogLevel? level = null,
string machineName = null,
string environment = null,
string application = null,
@ -278,60 +267,70 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
int? threadId = null,
bool? hasException = null)
{
var querys = new List<Func<QueryContainerDescriptor<SerilogInfo>, QueryContainer>>();
var queries = new List<Query>();
if (_currentTenant.IsAvailable)
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.TenantId))).Value(_currentTenant.GetId())));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.TenantId)), _currentTenant.GetId().ToString()));
}
if (startTime.HasValue)
{
querys.Add((log) => log.DateRange((q) => q.Field(GetField(nameof(SerilogInfo.TimeStamp))).GreaterThanOrEquals(_clock.Normalize(startTime.Value))));
queries.Add(new DateRangeQuery(GetField(nameof(SerilogInfo.TimeStamp)))
{
Gte = _clock.Normalize(startTime.Value),
});
}
if (endTime.HasValue)
{
querys.Add((log) => log.DateRange((q) => q.Field(GetField(nameof(SerilogInfo.TimeStamp))).LessThanOrEquals(_clock.Normalize(endTime.Value))));
queries.Add(new DateRangeQuery(GetField(nameof(SerilogInfo.TimeStamp)))
{
Lte = _clock.Normalize(endTime.Value),
});
}
if (level.HasValue)
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Level))).Value(GetLogEventLevel(level.Value).ToString())));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Level)), GetLogEventLevel(level.Value).ToString()));
}
if (!machineName.IsNullOrWhiteSpace())
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.MachineName))).Value(machineName)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.MachineName)), machineName));
}
if (!environment.IsNullOrWhiteSpace())
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.Environment))).Value(environment)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.Environment)), environment));
}
if (!application.IsNullOrWhiteSpace())
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.Application))).Value(application)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.Application)), application));
}
if (!context.IsNullOrWhiteSpace())
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.Context))).Value(context)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.Context)), context));
}
if (!requestId.IsNullOrWhiteSpace())
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.RequestId))).Value(requestId)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.RequestId)), requestId));
}
if (!requestPath.IsNullOrWhiteSpace())
{
// 模糊匹配
querys.Add((log) => log.MatchPhrasePrefix((q) => q.Field(f => f.Fields.RequestPath).Query(requestPath)));
// 前缀匹配
queries.Add(new MatchPhrasePrefixQuery(GetField(nameof(SerilogInfo.Fields.RequestPath)), requestPath));
}
if (!correlationId.IsNullOrWhiteSpace())
{
querys.Add((log) => log.MatchPhrase((q) => q.Field(GetField(nameof(SerilogInfo.Fields.CorrelationId))).Query(correlationId)));
// 模糊匹配
queries.Add(new WildcardQuery(GetField(nameof(SerilogInfo.Fields.CorrelationId)))
{
Value = $"*{correlationId}*"
});
}
if (processId.HasValue)
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.ProcessId))).Value(processId)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.ProcessId)), FieldValue.FromValue(processId.Value)));
}
if (threadId.HasValue)
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Fields.ThreadId))).Value(threadId)));
queries.Add(new TermQuery(GetField(nameof(SerilogInfo.Fields.ThreadId)), FieldValue.FromValue(threadId.Value)));
}
if (hasException.HasValue)
@ -343,9 +342,7 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
"field": "exceptions"
}
*/
querys.Add(
(q) => q.Exists(
(e) => e.Field("exceptions")));
queries.Add(new ExistsQuery(GetField("Exceptions")));
}
else
{
@ -361,15 +358,17 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
]
}
*/
querys.Add(
(q) => q.Bool(
(b) => b.MustNot(
(m) => m.Exists(
(e) => e.Field("exceptions")))));
queries.Add(new BoolQuery
{
MustNot = new List<Query>
{
new ExistsQuery(GetField("Exceptions"))
}
});
}
}
return querys;
return queries;
}
protected virtual string CreateIndex(DateTimeOffset? offset = null)
@ -381,15 +380,15 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep
return string.Format(_options.IndexFormat, offset.Value).ToLowerInvariant();
}
protected virtual LogEventLevel GetLogEventLevel(Microsoft.Extensions.Logging.LogLevel logLevel)
protected virtual LogEventLevel GetLogEventLevel(LogLevel logLevel)
{
return logLevel switch
{
Microsoft.Extensions.Logging.LogLevel.None or Microsoft.Extensions.Logging.LogLevel.Critical => LogEventLevel.Fatal,
Microsoft.Extensions.Logging.LogLevel.Error => LogEventLevel.Error,
Microsoft.Extensions.Logging.LogLevel.Warning => LogEventLevel.Warning,
Microsoft.Extensions.Logging.LogLevel.Information => LogEventLevel.Information,
Microsoft.Extensions.Logging.LogLevel.Debug => LogEventLevel.Debug,
LogLevel.None or LogLevel.Critical => LogEventLevel.Fatal,
LogLevel.Error => LogEventLevel.Error,
LogLevel.Warning => LogEventLevel.Warning,
LogLevel.Information => LogEventLevel.Information,
LogLevel.Debug => LogEventLevel.Debug,
_ => LogEventLevel.Verbose,
};
}

18
aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogException.cs

@ -1,25 +1,27 @@
namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch;
using System.Text.Json.Serialization;
namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch;
public class SerilogException
{
[Nest.PropertyName("SourceContext")]
[JsonPropertyName("SourceContext")]
public int Depth { get; set; }
[Nest.PropertyName("ClassName")]
[JsonPropertyName("ClassName")]
public string Class { get; set; }
[Nest.PropertyName("Message")]
[JsonPropertyName("Message")]
public string Message { get; set; }
[Nest.PropertyName("Source")]
[JsonPropertyName("Source")]
public string Source { get; set; }
[Nest.PropertyName("StackTraceString")]
[JsonPropertyName("StackTraceString")]
public string StackTrace { get; set; }
[Nest.PropertyName("HResult")]
[JsonPropertyName("HResult")]
public int HResult { get; set; }
[Nest.PropertyName("HelpURL")]
[JsonPropertyName("HelpURL")]
public string HelpURL { get; set; }
}

33
aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogField.cs

@ -1,56 +1,57 @@
using LINGYUN.Abp.Serilog.Enrichers.Application;
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using System;
using System.Text.Json.Serialization;
namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch;
public class SerilogField
{
[Nest.PropertyName(AbpSerilogUniqueIdConsts.UniqueIdPropertyName)]
[JsonPropertyName(AbpSerilogUniqueIdConsts.UniqueIdPropertyName)]
public long UniqueId { get; set; }
[Nest.PropertyName(AbpLoggingEnricherPropertyNames.MachineName)]
[JsonPropertyName(AbpLoggingEnricherPropertyNames.MachineName)]
public string MachineName { get; set; }
[Nest.PropertyName(AbpLoggingEnricherPropertyNames.EnvironmentName)]
[JsonPropertyName(AbpLoggingEnricherPropertyNames.EnvironmentName)]
public string Environment { get; set; }
[Nest.PropertyName(AbpSerilogEnrichersConsts.ApplicationNamePropertyName)]
[JsonPropertyName(AbpSerilogEnrichersConsts.ApplicationNamePropertyName)]
public string Application { get; set; }
[Nest.PropertyName("SourceContext")]
[JsonPropertyName("SourceContext")]
public string Context { get; set; }
[Nest.PropertyName("ActionId")]
[JsonPropertyName("ActionId")]
public string ActionId { get; set; }
[Nest.PropertyName("ActionName")]
[JsonPropertyName("ActionName")]
public string ActionName { get; set; }
[Nest.PropertyName("RequestId")]
[JsonPropertyName("RequestId")]
public string RequestId { get; set; }
[Nest.PropertyName("RequestPath")]
[JsonPropertyName("RequestPath")]
public string RequestPath { get; set; }
[Nest.PropertyName("ConnectionId")]
[JsonPropertyName("ConnectionId")]
public string ConnectionId { get; set; }
[Nest.PropertyName("CorrelationId")]
[JsonPropertyName("CorrelationId")]
public string CorrelationId { get; set; }
[Nest.PropertyName("ClientId")]
[JsonPropertyName("ClientId")]
public string ClientId { get; set; }
[Nest.PropertyName("UserId")]
[JsonPropertyName("UserId")]
public string UserId { get; set; }
[Nest.PropertyName("TenantId")]
[JsonPropertyName("TenantId")]
public Guid? TenantId { get; set; }
[Nest.PropertyName("ProcessId")]
[JsonPropertyName("ProcessId")]
public int ProcessId { get; set; }
[Nest.PropertyName("ThreadId")]
[JsonPropertyName("ThreadId")]
public int ThreadId { get; set; }
}

11
aspnet-core/framework/logging/LINGYUN.Abp.Logging.Serilog.Elasticsearch/LINGYUN/Abp/AuditLogging/Serilog/Elasticsearch/SerilogInfo.cs

@ -2,24 +2,25 @@
using Serilog.Formatting.Elasticsearch;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace LINGYUN.Abp.Logging.Serilog.Elasticsearch;
[Serializable]
public class SerilogInfo
{
[Nest.PropertyName(ElasticsearchJsonFormatter.TimestampPropertyName)]
[JsonPropertyName(ElasticsearchJsonFormatter.TimestampPropertyName)]
public DateTime TimeStamp { get; set; }
[Nest.PropertyName(ElasticsearchJsonFormatter.LevelPropertyName)]
[JsonPropertyName(ElasticsearchJsonFormatter.LevelPropertyName)]
public LogEventLevel Level { get; set; }
[Nest.PropertyName(ElasticsearchJsonFormatter.RenderedMessagePropertyName)]
[JsonPropertyName(ElasticsearchJsonFormatter.RenderedMessagePropertyName)]
public string Message { get; set; }
[Nest.PropertyName("fields")]
[JsonPropertyName("fields")]
public SerilogField Fields { get; set; }
[Nest.PropertyName("exceptions")]
[JsonPropertyName("exceptions")]
public List<SerilogException> Exceptions { get; set; }
}

Loading…
Cancel
Save