|
|
@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
using Microsoft.Extensions.Options; |
|
|
using Microsoft.Extensions.Options; |
|
|
using Serilog.Events; |
|
|
using Serilog.Events; |
|
|
|
|
|
using Serilog.Formatting.Elasticsearch; |
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
@ -195,6 +196,10 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
(t) => t.Field(GetField(indexMapping, "fields.UniqueId")).Value(id))))) |
|
|
(t) => t.Field(GetField(indexMapping, "fields.UniqueId")).Value(id))))) |
|
|
.Size(1), |
|
|
.Size(1), |
|
|
cancellationToken); |
|
|
cancellationToken); |
|
|
|
|
|
if (response.TryGetErrorMessage(out var errorMessage)) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.LogWarning("Query logs failed: {errorMessage}", errorMessage); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return _objectMapper.Map<SerilogInfo?, LogInfo?>(response.Documents.FirstOrDefault()); |
|
|
return _objectMapper.Map<SerilogInfo?, LogInfo?>(response.Documents.FirstOrDefault()); |
|
|
@ -240,6 +245,10 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
dsl.Indices(indexName) |
|
|
dsl.Indices(indexName) |
|
|
.Query(log => log.Bool(b => b.Must(querys.ToArray()))), |
|
|
.Query(log => log.Bool(b => b.Must(querys.ToArray()))), |
|
|
cancellationToken); |
|
|
cancellationToken); |
|
|
|
|
|
if (response.TryGetErrorMessage(out var errorMessage)) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.LogWarning("Query log count failed: {errorMessage}", errorMessage); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return response.Count; |
|
|
return response.Count; |
|
|
} |
|
|
} |
|
|
@ -469,8 +478,9 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
} |
|
|
} |
|
|
}, cancellationToken); |
|
|
}, cancellationToken); |
|
|
|
|
|
|
|
|
if (!searchResponse.IsSuccess()) |
|
|
if (searchResponse.TryGetErrorMessage(out var errorMessage)) |
|
|
{ |
|
|
{ |
|
|
|
|
|
Logger.LogWarning("Query log failed: {errorMessage}", errorMessage); |
|
|
return []; |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -506,8 +516,9 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
.SearchAfter(searchAfter); |
|
|
.SearchAfter(searchAfter); |
|
|
}, cancellationToken); |
|
|
}, cancellationToken); |
|
|
|
|
|
|
|
|
if (!searchResponse.IsSuccess()) |
|
|
if (searchResponse.TryGetErrorMessage(out var errorMessage)) |
|
|
{ |
|
|
{ |
|
|
|
|
|
Logger.LogWarning("Query log failed: {errorMessage}", errorMessage); |
|
|
return []; |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -563,15 +574,15 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var remaining = skipCount - 10000; |
|
|
|
|
|
// 获取skipCount最近一条数据作为searchAfter
|
|
|
// 获取skipCount最近一条数据作为searchAfter
|
|
|
var secondResponse = await client.SearchAsync<SerilogInfo>( |
|
|
var secondResponse = await client.SearchAsync<SerilogInfo>( |
|
|
dsl => dsl.Indices(CreateIndex()) |
|
|
dsl => dsl.Indices(CreateIndex()) |
|
|
.Query(query) |
|
|
.Query(query) |
|
|
.Sort(sorts) |
|
|
// 反转排序取第一个数据作为起始索引
|
|
|
|
|
|
.Sort(sorts.Select(x => x).Reverse().ToArray()) |
|
|
.SourceIncludes(x => x.Level) |
|
|
.SourceIncludes(x => x.Level) |
|
|
.SearchAfter(firstHit.Sort.ToList()) |
|
|
.SearchAfter(firstHit.Sort.ToList()) |
|
|
.Size(remaining), |
|
|
.Size(1), |
|
|
cancellationToken); |
|
|
cancellationToken); |
|
|
|
|
|
|
|
|
if (!secondResponse.IsSuccess() || secondResponse.Hits == null || !secondResponse.Hits.Any()) |
|
|
if (!secondResponse.IsSuccess() || secondResponse.Hits == null || !secondResponse.Hits.Any()) |
|
|
@ -579,11 +590,6 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (secondResponse.Hits.Count < remaining) |
|
|
|
|
|
{ |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var lastHit = secondResponse.Hits.LastOrDefault(); |
|
|
var lastHit = secondResponse.Hits.LastOrDefault(); |
|
|
if (lastHit?.Sort == null || !lastHit.Sort.Any()) |
|
|
if (lastHit?.Sort == null || !lastHit.Sort.Any()) |
|
|
{ |
|
|
{ |
|
|
@ -621,32 +627,29 @@ public class SerilogElasticsearchLoggingManager : ILoggingManager, ISingletonDep |
|
|
? SortOrder.Asc : SortOrder.Desc; |
|
|
? SortOrder.Asc : SortOrder.Desc; |
|
|
sorting = !sorting.IsNullOrWhiteSpace() |
|
|
sorting = !sorting.IsNullOrWhiteSpace() |
|
|
? sorting.Split()[0] |
|
|
? sorting.Split()[0] |
|
|
: nameof(SerilogInfo.TimeStamp); |
|
|
: ElasticsearchJsonFormatter.TimestampPropertyName; |
|
|
|
|
|
|
|
|
SortOptions[]? sorts = null; |
|
|
SortOptions[]? sorts = null; |
|
|
if (sorting.IsNullOrWhiteSpace()) |
|
|
var sortingFieldMap = indexMappingInfo.Fields |
|
|
|
|
|
.Where(x => x.Key.Equals(sorting, StringComparison.CurrentCultureIgnoreCase)) |
|
|
|
|
|
.Select(x => x.Value) |
|
|
|
|
|
.FirstOrDefault(); |
|
|
|
|
|
if (sortingFieldMap != null) |
|
|
{ |
|
|
{ |
|
|
var sortingFieldMap = indexMappingInfo.Fields |
|
|
sorting = sortingFieldMap.Path; |
|
|
.Where(x => x.Key.Equals(sorting, StringComparison.CurrentCultureIgnoreCase)) |
|
|
} |
|
|
.Select(x => x.Value) |
|
|
if (!sorting.IsNullOrWhiteSpace()) |
|
|
.FirstOrDefault(); |
|
|
{ |
|
|
if (sortingFieldMap != null) |
|
|
sorts = new SortOptions[1] |
|
|
{ |
|
|
|
|
|
sorting = sortingFieldMap.Path; |
|
|
|
|
|
} |
|
|
|
|
|
if (!sorting.IsNullOrWhiteSpace()) |
|
|
|
|
|
{ |
|
|
{ |
|
|
sorts = new SortOptions[1] |
|
|
new SortOptions |
|
|
{ |
|
|
{ |
|
|
new SortOptions |
|
|
Field = new FieldSort(new Field(sorting)) |
|
|
{ |
|
|
{ |
|
|
Field = new FieldSort(new Field(sorting)) |
|
|
Order = sortOrder, |
|
|
{ |
|
|
}, |
|
|
Order = sortOrder, |
|
|
} |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return sorts; |
|
|
return sorts; |
|
|
|