asp.net-coreserilogasp.net-core-5.0

Serilog Console sink output newline as \r\n


I am using Serilog logger together with a Console sink to do logging in my ASP.NET Core 5.0 project. Also use the same ILoggerFactory with EF Core 5.0 like below

optionsBuilder.UseLoggerFactory(serilogFactory) 

But when EF Core logs SQL statements, the lines are not shown as linebreak on the console, instead it is shown as \r\n instead. But if I use the builtin LoggerFactory from ASP.NET Core like below

var loggerFactory = LoggerFactory.Create(
    builder => builder.AddConsole(options =>
    {
    })
);
optionsBuilder.UseLoggerFactory(loggerFactory);

Then it's fine.

Is there a way to fix that?


Solution

  • When using Serilog with the Console Sink the Message property in the Template should have the format specifier :l to achieve the desired format:

    "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:l}{NewLine}{Exception}"
    

    By using that specifier the message string won't be quoted, thus formatting the output correctly.

    More info about specifiers: https://github.com/serilog/serilog/wiki/Formatting-Output