Skip to content

Commit c3e9548

Browse files
committed
Feat: TruncateSourceContextEnricher
1 parent de60033 commit c3e9548

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/CodeOfChaos.Extensions.Serilog/Enrichers/PaddedSectionEnricher.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ namespace CodeOfChaos.Extensions.Serilog.Enrichers;
99
// Code
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
public class PaddedSectionEnricher : ILogEventEnricher {
12-
public const int MaxLength = 8;
12+
public int MaxLength { get; } = 8;
13+
14+
// -----------------------------------------------------------------------------------------------------------------
15+
// Constructors
16+
// -----------------------------------------------------------------------------------------------------------------
17+
public PaddedSectionEnricher() { }
18+
public PaddedSectionEnricher(int maxLength) => MaxLength = maxLength;
1319

1420
// -----------------------------------------------------------------------------------------------------------------
1521
// Methods
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Serilog.Core;
5+
using Serilog.Events;
6+
7+
namespace CodeOfChaos.Extensions.Serilog.Enrichers;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class TruncateSourceContextEnricher : ILogEventEnricher {
12+
public int MaxLength { get; } = 8;
13+
14+
// -----------------------------------------------------------------------------------------------------------------
15+
// Constructors
16+
// -----------------------------------------------------------------------------------------------------------------
17+
public TruncateSourceContextEnricher() { }
18+
public TruncateSourceContextEnricher(int maxLength) => MaxLength = maxLength;
19+
20+
// -----------------------------------------------------------------------------------------------------------------
21+
// Methods
22+
// -----------------------------------------------------------------------------------------------------------------
23+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) {
24+
if (!logEvent.Properties.TryGetValue("SourceContext", out LogEventPropertyValue? sourceContextValue)
25+
|| sourceContextValue is not ScalarValue { Value: string sourceContext }) return;
26+
27+
string truncatedSourceContext = sourceContext.Length > MaxLength + 3
28+
? string.Concat("...", sourceContext.AsSpan(sourceContext.Length - MaxLength, MaxLength))
29+
: sourceContext;
30+
31+
var truncatedProperty = new LogEventProperty("SourceContext", new ScalarValue(truncatedSourceContext));
32+
logEvent.AddOrUpdateProperty(truncatedProperty);
33+
}
34+
}

src/CodeOfChaos.Extensions.Serilog/LoggerConfigurationExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public static LoggerConfiguration WriteToAsyncConsole(
3737
return loggerConfiguration;
3838
}
3939

40-
public static LoggerConfiguration WithPaddedSectionEnricher(this LoggerConfiguration loggerConfiguration) {
41-
return loggerConfiguration.Enrich.With<PaddedSectionEnricher>();
42-
}
40+
public static LoggerConfiguration WithPaddedSectionEnricher(this LoggerConfiguration loggerConfiguration, int maxLength = 8)
41+
=> loggerConfiguration.Enrich.With(new PaddedSectionEnricher(maxLength));
42+
43+
public static LoggerConfiguration WithTruncateSourceContextEnricher(this LoggerConfiguration loggerConfiguration, int maxLength = 8)
44+
=> loggerConfiguration.Enrich.With(new TruncateSourceContextEnricher(maxLength));
4345

4446
// -----------------------------------------------------------------------------------------------------------------
4547
// Opinionated configurations

0 commit comments

Comments
 (0)