I was able to reproduce the issue and - whether this is right or not - the 
microsoft developers have decided that changes in the system settings are not 
pushed to running (.NET) processes. Instead every process caches culture 
information and the cached information is used until it is explicitly 
refreshed. So the issue you observed is not something that affects only 
log4net, but it is something that affects all .NET applications around the 
globe. If you're happy with that explanation don't read on. More fancy stuff 
follows.

First I did not believe it would be so. Thus I wrote a small test application 
that does not much but display the current time:

        public partial class MainWindow : Window
        {
                private static readonly ILog log = 
LogManager.GetLogger(typeof(MainWindow));

                static MainWindow()
                {
                        BasicConfigurator.Configure();
                }

                public MainWindow()
                {
                        InitializeComponent();

                        DispatcherTimer dt = new DispatcherTimer();
                        dt.Interval = TimeSpan.FromSeconds(1);
                        dt.Tick += dt_Tick;
                        dt.Start();
                }

                void dt_Tick(object sender, EventArgs e)
                {
                        if (ClearCachedDataCheckBox.IsChecked.Value)
                        {
                                CultureInfo.CurrentCulture.ClearCachedData();
                        }

                        DateTime localNow = DateTime.Now;
                        DateTime utcNow = DateTime.UtcNow;

                        string localNowString = 
localNow.ToString(CultureInfo.InvariantCulture);
                        string utcNowString = 
utcNow.ToString(CultureInfo.InvariantCulture);

                        log.InfoFormat("Local time='{0}'; UTC time='{1}'", 
localNowString, utcNowString);
                        LocalTimeLabel.Content = localNowString;
                        UtcTimeLabel.Content = utcNowString;
                }
        } 

So I could see that the time did indeed not update itself until I checked the 
"ClearCachedDataCheckBox" and "ClearCachedData" was invoked. And I can also 
understand why microsoft decided to make it behave like this. Imagine what 
would happen if your program depends on an ever-increasing timestamp from 
DateTime.Now. I.e. something like:

DateTime lastTime = DateTime.MinValue;
while(true) {
        if(lastTime + TimeSpan.FromMinutes(1) < DateTime.Now) {
                try {
                        // do an important operation every 1 minutes
                } finally {
                        lastTime = DateTime.Now;
                }
        }
}

This going to fail if someone changes the timezone so that the local time goes 
into the past. Your code won't be executed for the amount of hours he travelled 
back into the past. Of course one could work around this issue by comparing the 
UNIX timestamp and doing modulo operations, but there are a lot of lazy 
developers on earth and the real world is a messy place. ;-) Please note also 
that my example is a simple single-threaded one. The above might not work 
anymore as soon as you start with multi-threading. The CultureInfo properties 
are, AFAIK, thread-static.

Cheers,
D.

-----Ursprüngliche Nachricht-----
Von: Dominik Psenner [mailto:dpsen...@gmail.com] 
Gesendet: Donnerstag, 23. Mai 2013 13:13
An: 'Log4NET User'
Betreff: AW: How to force log4net to refresh timezone information?

In aircrafts the UTC timezone is used. And one is not allowed to use laptops 
during starts and landings. :-)

To come back to topic: is the time zone changed within an application or via 
the windows control panel that can be opened by following the guide below?

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/windows_date_change_time.mspx?mfr=true

-----Ursprüngliche Nachricht-----
Von: Christian Grobmeier [mailto:grobme...@gmail.com] 
Gesendet: Donnerstag, 23. Mai 2013 12:00
An: Log4NET User
Betreff: Re: How to force log4net to refresh timezone information?

Hi,

On Thu, May 23, 2013 at 11:31 AM, Dominik Psenner <dpsen...@gmail.com> wrote:
> I’m wondering, how often does your deployment computer travel from one
> timezone to another while it is powered on?

Maybe its an app for use in aircraft? You never know...



> Von: Warrick Wilson [mailto:guywithd...@gmail.com]
> Gesendet: Donnerstag, 23. Mai 2013 05:55
> An: log4net-user@logging.apache.org
> Betreff: How to force log4net to refresh timezone information?
>
> I've inherited a project that users log4net to do some logging. One of the
> things that got noticed was that changes to the timezone on the PC that the
> program runs on don't get the logging times changed (e.g. changing the
> timezone from Pacific to Eastern does not change the logging time by 3
> hours)
>
>
>
> The program code that was written by "us" has routines in that trap an event
> for timezone changes and call TimeZoneInfo.ClearCachedData() (something like
> that... doing this from memory).
>
>
>
> Is there a call that can be made to get the log4net "innards" to do the same
> sort of thing?



--
http://www.grobmeier.de
https://www.timeandbill.de


Reply via email to