示例#1
0
 static void setRootDir(bool create)
 {
     lock (lockObject)
     {
         if (rootDir != null)
         {
             if (!create)
             {
                 return;
             }
             if (Directory.Exists(rootDir))
             {
                 return;
             }
         }
         List <string> baseDirs = new List <string> {
             CompanyUserDataDir,
             CompanyCommonDataDir,
             Log.AppDir,
             Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
             Path.GetTempPath() + Path.DirectorySeparatorChar + CompanyName + Path.DirectorySeparatorChar,
         };
         if (Log.baseDirs != null)
         {
             baseDirs.InsertRange(0, Log.baseDirs);
         }
         foreach (string baseDir in baseDirs)
         {
             BaseDir = baseDir;
             rootDir = BaseDir + Path.DirectorySeparatorChar + rootDirName + RootDirNameSuffix;
             if (create)
             {
                 try
                 {
                     if (!Directory.Exists(rootDir))
                     {
                         FileSystemRoutines.CreateDirectory(rootDir);
                     }
                     string testFile = rootDir + Path.DirectorySeparatorChar + "test";
                     File.WriteAllText(testFile, "test");
                     File.Delete(testFile);
                     break;
                 }
                 catch //(Exception e)
                 {
                     rootDir = null;
                 }
             }
         }
         if (rootDir == null)
         {
             throw new Exception("Could not access any log directory.");
         }
         rootDir = PathRoutines.GetNormalizedPath(rootDir, false);
         if (Directory.Exists(rootDir) && deleteLogsOlderThanDays >= 0 && deletingOldLogsThread?.IsAlive != true)
         {
             deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderThanDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread
         }
     }
 }
示例#2
0
 protected static string get_normalized_directory(string directory = null)
 {
     if (directory == null)
     {
         directory = Cliver.Log.AppCommonDataDir;
     }
     return(PathRoutines.GetNormalizedPath(directory, true));
 }
示例#3
0
 static void setWorkDir(bool create)
 {
     lock (lockObject)
     {
         if (workDir != null)
         {
             if (!create)
             {
                 return;
             }
             if (Directory.Exists(workDir))
             {
                 return;
             }
         }
         List <string> baseDirs = new List <string> {
             Log.AppDir,
             CompanyUserDataDir,
             CompanyCommonDataDir,
             Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
             System.IO.Path.GetTempPath() + System.IO.Path.DirectorySeparatorChar + CompanyName + System.IO.Path.DirectorySeparatorChar,
         };
         if (Log.primaryBaseDirs != null)
         {
             baseDirs.InsertRange(0, Log.primaryBaseDirs);
         }
         foreach (string baseDir in baseDirs)
         {
             workDir = baseDir + System.IO.Path.DirectorySeparatorChar + Log.ProcessName + WorkDirNameSuffix;
             if (create)
             {
                 try
                 {
                     if (!Directory.Exists(workDir))
                     {
                         FileSystemRoutines.CreateDirectory(workDir);
                     }
                     string testFile = workDir + System.IO.Path.DirectorySeparatorChar + "test";
                     File.WriteAllText(testFile, "test");
                     File.Delete(testFile);
                     Log.baseDir = baseDir;
                     break;
                 }
                 catch //(Exception e)
                 {
                     workDir = null;
                 }
             }
         }
         if (workDir == null)
         {
             throw new Exception("Could not access any log directory.");
         }
         workDir = PathRoutines.GetNormalizedPath(workDir, false);
         if (Directory.Exists(workDir) && deleteLogsOlderDays >= 0)
         {
             if (deletingOldLogsThread?.TryAbort(1000) == false)
             {
                 throw new Exception("Could not abort deletingOldLogsThread");
             }
             deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread
         }
         else
         {
             throw new Exception("Could not create log folder!");
         }
     }
     // deletingOldLogsThread?.Join();
 }