Skip to content

Commit 65baa3b

Browse files
committed
parameterize original log event
1 parent 6acadb4 commit 65baa3b

File tree

5 files changed

+9
-3
lines changed

5 files changed

+9
-3
lines changed

src/SeqCli/Ingestion/JsonLogEventReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static LogEvent ReadFromJObject(JObject jObject)
7777
if (serilogLevel != LogEventLevel.Information)
7878
jObject.Add("@l", new JValue(serilogLevel.ToString()));
7979

80-
jObject.Add("SeqCliOriginalLevel", originalLevel);
80+
jObject.Add(LevelMapping.SurrogateLevelProperty, originalLevel);
8181
}
8282

8383
return LogEventReader.ReadFromJObject(jObject);

src/SeqCli/Levels/LevelMapping.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace SeqCli.Levels;
2020

2121
public static class LevelMapping
2222
{
23+
// Use a "hygienic" name for the original level value to avoid collisions
24+
internal static readonly string SurrogateLevelProperty = $"_SeqcliOriginalLevel_{Guid.NewGuid():N}";
25+
2326
static readonly Dictionary<string, (string, LogEventLevel)> LevelsByName =
2427
new(StringComparer.OrdinalIgnoreCase)
2528
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SeqCli.Levels;
12
using Serilog.Formatting;
23
using Serilog.Templates;
34

@@ -6,6 +7,6 @@ namespace SeqCli.Output;
67
static class OutputFormatter
78
{
89
internal static readonly ITextFormatter Json = new ExpressionTemplate(
9-
"{ {@t, @mt, @l: coalesce(SeqCliOriginalLevel, if @l = 'Information' then undefined() else @l), @x, @sp, @tr, @ps, @st, ..rest()} }\n"
10+
$"{{ {{@t, @mt, @l: coalesce({LevelMapping.SurrogateLevelProperty}, if @l = 'Information' then undefined() else @l), @x, @sp, @tr, @ps, @st, ..rest()}} }}\n"
1011
);
1112
}

src/SeqCli/PlainText/LogEvents/LogEventBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static LogEvent FromProperties(IDictionary<string, object?> properties, s
3838
var props = GetLogEventProperties(properties, remainder, level);
3939

4040
var fallbackMappedLevel = level != null ? LevelMapping.ToSerilogLevel(level) : LogEventLevel.Information;
41-
properties["SeqCliOriginalLevel"] = level;
41+
properties[LevelMapping.SurrogateLevelProperty] = level;
4242

4343
return new LogEvent(
4444
timestamp,

test/SeqCli.EndToEnd/Ingest/LevelledTextIngestionTestCase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ public async Task ExecuteAsync(
2727
// Since @l is a token, no level mapping is applied
2828
Assert.Equal(1, events.Count(e => e.Level == "INFO"));
2929
Assert.Equal(1, events.Count(e => e.Level == "WARN"));
30+
31+
Assert.DoesNotContain(events, e => e.Properties.Any(p => p.Name.StartsWith("_Seqcli")));
3032
}
3133
}

0 commit comments

Comments
 (0)