Strip logfile of markup.

This commit is contained in:
2025-08-19 13:45:38 +01:00
parent 1ade4b4e80
commit f57a5635d9

View File

@@ -3,6 +3,7 @@ using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.Spectre;
using Spectre.Console;
using Spectre.Console.Cli;
public class LoggingInterceptor : ICommandInterceptor
@@ -39,9 +40,10 @@ public class LoggingInterceptor : ICommandInterceptor
if(!string.IsNullOrWhiteSpace(global.LogFile))
{
loggerConfig = loggerConfig.Enrich.FromLogContext()
.WriteTo.File(global.LogFile,
outputTemplate:
"[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");
.WriteTo.Logger(lc => lc.Enrich.With<StripMarkupEnricher>()
.WriteTo.File(global.LogFile,
outputTemplate:
"[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {CleanMessage:lj}{NewLine}{Exception}"));
}
Log.Logger = loggerConfig.CreateLogger();
@@ -49,5 +51,29 @@ public class LoggingInterceptor : ICommandInterceptor
if(global.LogFile != null) Log.Information("Logging to file: {Path}", global.LogFile);
}
#endregion
#region Nested type: StripMarkupEnricher
public class StripMarkupEnricher : ILogEventEnricher
{
#region ILogEventEnricher Members
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
// Render the full message (with all tokens applied)
string rendered = logEvent.RenderMessage();
// Remove HTML tags
string cleaned = Markup.Remove(rendered);
// Attach a new property CleanMessage
LogEventProperty prop = propertyFactory.CreateProperty("CleanMessage", cleaned);
logEvent.AddOrUpdateProperty(prop);
}
#endregion
}
#endregion
}