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,271
  • 16
  • 31

0 Answers0