Skip to content

Commit

Permalink
Merge pull request #911 from colinin/fixed-serilog-loglevel-mapping
Browse files Browse the repository at this point in the history
Fix the serilog log level mapping
  • Loading branch information
colinin authored Nov 30, 2023
2 parents 828f136 + 0a989f2 commit 99bc387
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Nest;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -293,7 +294,7 @@ protected virtual List<Func<QueryContainerDescriptor<SerilogInfo>, QueryContaine
}
if (level.HasValue)
{
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Level))).Value(level.ToString())));
querys.Add((log) => log.Term((q) => q.Field(GetField(nameof(SerilogInfo.Level))).Value(GetLogEventLevel(level.Value).ToString())));
}
if (!machineName.IsNullOrWhiteSpace())
{
Expand Down Expand Up @@ -380,6 +381,19 @@ protected virtual string CreateIndex(DateTimeOffset? offset = null)
return string.Format(_options.IndexFormat, offset.Value).ToLowerInvariant();
}

protected virtual LogEventLevel GetLogEventLevel(Microsoft.Extensions.Logging.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,
_ => LogEventLevel.Verbose,
};
}

private readonly static IDictionary<string, string> _fieldMaps = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
{
{ "timestamp", "@timestamp" },
Expand Down

0 comments on commit 99bc387

Please sign in to comment.