Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Serilog to print special characters correctly?

I use Serilog as a rolling file logger. At the start of a console application I call this custom extension method on ILogger.

        public static void LogStartExecution<TCategoryName>(this ILogger<TCategoryName> logger)
        {
            logger.LogInformation("DÉBUT Traitement {0}", typeof(TCategoryName));
        }

        public static void ConfigureLogging(IServiceCollection services, LoggingConfig loggingConfig)
        {
            // Initialize serilog logger
            Log.Logger = new LoggerConfiguration()
                 .WriteTo.File(path: @$"{loggingConfig.Path}\{loggingConfig.FileName}",
                               rollingInterval: RollingInterval.Day,
                               rollOnFileSizeLimit: true,
                               fileSizeLimitBytes: 5000000,
                               retainedFileCountLimit: 100)
                 .MinimumLevel.Debug()
                 .Enrich.FromLogContext()
                 .CreateLogger();

            // Add logging
            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConsole();
                loggingBuilder.AddSerilog();
                loggingBuilder.AddDebug();
            });
        }

The resulting output is like so : 2020-05-19 19:39:56.158 -04:00 [INF] DÉBUT Traitement Xnt.Annotation.Retrait.TraitementRetrait

The special character 'É' appears like gibberish.

How can I configure Serilog to output special characters correctly. Culture should be fr-CA

like image 552
papps Avatar asked Jan 22 '26 21:01

papps


1 Answers

Adding the parameter 'encoding: Encoding.UTF8' solves the issue.

.Logger = new LoggerConfiguration()
   .WriteTo.File(path: @$"{loggingConfig.Path}\{loggingConfig.FileName}",
                 rollingInterval: RollingInterval.Day,
                 rollOnFileSizeLimit: true,
                 fileSizeLimitBytes: 5000000,
                 retainedFileCountLimit: 100,
                 encoding: Encoding.UTF8)
like image 72
papps Avatar answered Jan 25 '26 15:01

papps