1

I'm currently working on a .NET Core Windows Service, implementing a couple of BackgroundService classes to run stuff on a timer.

All is working perfectly bar one thing - writing a date to the ILogger reference appears to be using an incorrect Culture (en-US) rather then the system-configured en-AU.

For example -

    public override Task StartAsync(CancellationToken cancellationToken)
    {
        var utcNow = DateTime.UtcNow;
        Logger.LogInformation("Started at {utcNow}", utcNow);
        return base.StartAsync(cancellationToken);
    }

Will produce the log -

enter image description here

Is there a way in a Windows Service/Console app to set the Culture? I have used UseRequestLocalization ASP.NET Environment but it didn't seem logical in context as there would be no request.

I have tried setting all kinds of CultureInfo... properties with no result - any help would be appreciated.

Entry point Program class for reference -

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {

        return Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureLogging((hostContext, logging) =>
                {
                    DBLoggerConfiguration config = hostContext.Configuration.GetSection("Logging").Get<DBLoggerConfiguration>();
                    logging.AddDBLogger(config);
                })
                .ConfigureServices((hostContext, services) =>
                {

                    services.AddHttpClient<ISMSService, TwilioSMSService>();
                    services.AddHostedService<ExpirationSchedule>();
                    services.AddHostedService<BillingSchedule>();
                });
    }
}

EDIT: It appears through some digging in the Github repo the LogValuesFormatter responsible for formatting parameters within the message only uses CultureInfo.InvariantCulture - which leans heavily on the American way of formatting dates etc. There appears to be no way to configure this, so I have submitted a bug in the Github repo which will hopefully get some backing - https://github.com/dotnet/extensions/issues/3380

m.t.bennett
  • 1,290
  • 16
  • 34

1 Answers1

0

Apply this setting at this point!

    public static void Main(string[] args)
    {
      CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR", true) { DateTimeFormat = { ShortDatePattern = "dd/MM/yyyy", FullDateTimePattern = "dd/MM/yyyy HH:mm:ss", LongTimePattern = "HH:mm:ss" } };
    
      CreateHostBuilder(args).Build().Run();
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '22 at 12:51
  • Have a read - https://github.com/dotnet/runtime/issues/40242 - in short this won't work as the Logger doesn't adhere to any culture (yet). Likely implemented in .NET 7 – m.t.bennett Oct 06 '22 at 03:32