After digging through the source code of Serilog, I found where the shorthand formatting for u3, w3 etc was happening and submitted a PR so that 5 character length log levels would be output as I needed.
After a polite response from Serilog maintainers, they gave me a much simpler solution of implementing my own ILogEventEnricher
to add a new property to the log such as {Log4NetLevel}
where the Enricher code can map Serilog Levels to be output in the exact format that Log4Net would use.
Then I can update my outputTemplate to use my new property {Log4NetLevel}
as opposed to {Level:u5}
Here is a basic example
/// <summary>
/// This is used to create a new property in Logs called 'Log4NetLevel'
/// So that we can map Serilog levels to Log4Net levels - so log files stay consistent
/// </summary>
public class Log4NetLevelMapperEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var log4NetLevel = string.Empty;
switch (logEvent.Level)
{
case LogEventLevel.Debug:
log4NetLevel = "DEBUG";
break;
case LogEventLevel.Error:
log4NetLevel = "ERROR";
break;
case LogEventLevel.Fatal:
log4NetLevel = "FATAL";
break;
case LogEventLevel.Information:
log4NetLevel = "INFO";
break;
case LogEventLevel.Verbose:
log4NetLevel = "ALL";
break;
case LogEventLevel.Warning:
log4NetLevel = "WARN";
break;
}
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Log4NetLevel", log4NetLevel));
}
}
Then I simply need to update my Serilog configuration to use my enricher, a much cleaner solution.
Thanks Serilog team!
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.With<Log4NetLevelMapperEnricher>()
.WriteTo.File(
$@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\UmbracoTraceLog.{Environment.MachineName}.txt",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Debug,
retainedFileCountLimit: null,
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [P{ProcessId}/D{AppDomainId}/T{ThreadId}] {Log4NetLevel} {Message:lj}{NewLine}{Exception}")
.CreateLogger();