/// <summary> /// Entry point for the timer reloading the configuration file. /// </summary> /// <param name="state">Some state object (not used).</param> private void TimerProc(object state) { lock (Sync) { if (mReloadingTimer == null) { return; // object has been disposed } try { // load file (always replace mFile, do not modify existing instance for threading reasons) File = LogConfigurationFile.LoadFrom(FullPath); } catch (FileNotFoundException) { // file does not exist, should be handled using file system notifications // => don't do anything here... } catch (Exception ex) { sLog.ForceWrite(LogLevel.Error, "Reloading configuration file failed. Exception: {0}", ex); mReloadingTimer.Change(500, -1); // try again later... } } }
/// <summary> /// Tries to open the configured configuration file. /// </summary> private void OpenConfigurationFile() { lock (Sync) { // dispose resources associated with the loaded configuration file CloseConfigurationFile(); // load configuration file const int maxRetryCount = 5; for (int retry = 0; retry < maxRetryCount; retry++) { try { File = LogConfigurationFile.LoadFrom(FullPath); break; } catch (FileNotFoundException) { // file does not exist // => that's ok, use a default configuration file... File = new LogConfigurationFile(); break; } catch (IOException) { // there is something wrong at a lower level, most probably a sharing violation // => just try again... if (retry + 1 >= maxRetryCount) { throw; } Thread.Sleep(10); } catch (Exception ex) { // a severe error that cannot be fixed here // => abort sLog.ForceWrite( LogLevel.Error, "Loading log configuration file ({0}) failed. Exception: {1}", FullPath, ex); throw; } } // set up the file system watcher to get notified of changes to the file // (notifications about the creation of files with zero-length do not contain // valuable information, so renaming/deleting is sufficient) mFileSystemWatcher = new FileSystemWatcher { Path = System.IO.Path.GetDirectoryName(FullPath), Filter = "*" + System.IO.Path.GetExtension(mFileName) }; mFileSystemWatcher.Changed += EH_FileSystemWatcher_Changed; mFileSystemWatcher.Deleted += EH_FileSystemWatcher_Removed; mFileSystemWatcher.Renamed += EH_FileSystemWatcher_Renamed; mFileSystemWatcher.EnableRaisingEvents = true; // set up timer that will handle reloading the configuration file mReloadingTimer = new Timer(TimerProc, null, -1, -1); // do not start immediately } }