Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1.         //*DHB++ 10/26/2010 WPL013419 - Improved upgrader logging
  2.         /// <summary>
  3.         /// Creates the new log file if needed.
  4.         /// </summary>
  5.         /// <param name="maxDebugFileSize">Max size of the debug file.</param>
  6.         public static void CreateNewLogFileIfNeeded(double maxDebugFileSize)
  7.         {
  8.             // Don't proceed if the max size is zero (no limit) or if the
  9.             // debug file doesn't exist, or we don't know the location of it...
  10.             if (maxDebugFileSize > 0.0 && !string.IsNullOrEmpty(m_sDebugFile) && File.Exists(m_sDebugFile))
  11.             {
  12.                 // vars to keep track of actions
  13.                 bool bBackupFileExisted = false;
  14.                 bool bBackupDeleted     = false;
  15.                 // setup the backup file path and file info
  16.                 string sBackupFilePath = m_sDebugFile.Substring(0, m_sDebugFile.LastIndexOf('.')) + BACKUP_EXTENSION;
  17.                 FileInfo oCurrentLogFileInfo = new FileInfo(m_sDebugFile);
  18.                 // check if we need to do the update yet.
  19.                 if (((double)oCurrentLogFileInfo.Length * BYTES_IN_MEGABYTE) >= maxDebugFileSize)
  20.                 {
  21.                     // the backup file exists.
  22.                     if (File.Exists(sBackupFilePath))
  23.                     {
  24.                         bBackupFileExisted = true;
  25.                         // try to delete the file.  If it succeeded, mark that it did.  If it didn't, log the failure.
  26.                         try
  27.                         {
  28.                             File.Delete(sBackupFilePath);
  29.                             bBackupDeleted = true;
  30.                         }
  31.                         catch
  32.                         {
  33.                             LogInfo("Deletion of backup log file " + sBackupFilePath + " could not occur.  It was readonly, locked by another process, or upgrader did not have permissions to delete this file.");
  34.                         }
  35.                     }
  36.                     // don't try to make the backup, and delete the current, if we couldn't delete the backup.
  37.                     // unless, of course, the backup didn't exist to be deleted.
  38.                     if (bBackupDeleted || !bBackupFileExisted)
  39.                     {
  40.                         // try to move the "current" log file to be the backup.  If we move it, it'll
  41.                         // just be created later, so we don't need to re-create it, or copy instead.
  42.                         try
  43.                         {
  44.                             oCurrentLogFileInfo.MoveTo(sBackupFilePath);
  45.                         }
  46.                         catch
  47.                         {
  48.                             LogInfo("Creation of backup log file " + sBackupFilePath + " could not occur. It's possible that upgrader did not have permissions to create this file.");
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement