public static void WriteErrorLog(SyncConfig confInstance, AppError error)
        {
            string errorLogLocation = confInstance.Parameters["LogFolder"];
            string errorLogName     = confInstance.ErrorLogFile;

            if (!Directory.Exists(errorLogLocation))
            {
                Directory.CreateDirectory(errorLogLocation);
            }

            //using (StreamWriter sw = File.AppendText(errorLogName))
            //{
            //    sw.WriteLine("Logging started on " + DateTime.Now.
            //        ToString(CultureInfo.InvariantCulture));
            //    sw.WriteLine("\n\n");
            //}

            // construct log text line
            string timeStamp   = error.DateTime.ToShortDateString() + error.DateTime.ToLongTimeString();
            string logTextLine = timeStamp + spaceSeparator
                                 + error.Method + spaceSeparator
                                 + error.Entry + spaceSeparator
                                 + error.ErrorMessage;

            using (StreamWriter sw = File.AppendText(errorLogName))
            {
                sw.WriteLine(logTextLine);
            }
        }
        public void AppendActionListWithUpdateCreateMove()
        {
            Console.WriteLine("\n");
            Console.WriteLine("Calculating necessary actions to perform...");
            int count = 0;

            foreach (var filePair in FileMappingFromPaths)
            {
                count++;
                try
                {
                    bool create = AddCreateAction(filePair);
                    if (create)
                    {
                        continue;
                    }


                    AddMoveAction(filePair);
                    AddUpdateAction(filePair);
                }
                catch (Exception e)
                {
                    // construct AppError
                    string entry = "trying to identify action. File1: " + filePair.Key.fullPath
                                   + "; File2: " + filePair.Value.fullPath;
                    string method = MethodBase.GetCurrentMethod().ToString();
                    var    error  = new AppError(DateTime.Now, method, entry, e.Message);
                    ErrorHandling.WriteErrorLog(SyncConfig, error);
                }


                //Init.DisplayCompletionInfo("entries processed from file mapping", count,FileMapping.Count);
            }

            foreach (var filePair in FileMappingFromCsv)
            {
                count++;
                try
                {
                    AddUpdateAction(filePair);
                }
                catch (Exception e)
                {
                    // construct AppError
                    string entry = "trying to identify action. File1: " + filePair.Key.fullPath
                                   + "; File2: " + filePair.Value.fullPath;
                    string method = MethodBase.GetCurrentMethod().ToString();
                    var    error  = new AppError(DateTime.Now, method, entry, e.Message);
                    ErrorHandling.WriteErrorLog(SyncConfig, error);
                }
                //Init.DisplayCompletionInfo("entries processed from file mapping", count, FileMapping.Count);
            }
            _actionList.Sort();
            CalculateSpaceNeeded();
            Console.WriteLine("Done.");
        }
Ejemplo n.º 3
0
        private static Dictionary <string, FileExtended> GetFilesDictionary(SyncConfig confInstance, string path, List <string> filesPaths, FileType fileType)
        {
            bool someFilesFailed = false;
            var  filesInFolder   = filesPaths.FindAll(x => x.Contains(path));
            int  filesAdded      = 0;
            int  totalFilesCount = filesPaths.Count;

            FileExtended[] filesArray = new FileExtended[totalFilesCount];

            foreach (var filePath in filesInFolder)
            {
                var timeStamp = DateTime.Now;
                try
                {
                    if (filePath.Length >= 260)
                    {
                        var error = new AppError(timeStamp, "InitializeFiles",
                                                 filePath,
                                                 "the file path length greater than 260 is not supported by the app. Move it up the hierarchy or rename to a shorter name");
                        ErrorHandling.WriteErrorLog(confInstance, error);
                        someFilesFailed = true;
                        continue;
                    }
                    var fileInfo     = new FileInfo(filePath);
                    var fileExtended = new FileExtended
                                       (
                        fileType,
                        path,
                        fileInfo.FullName,
                        Kernel32.GetCustomFileId(filePath)
                                       );

                    filesArray[filesAdded] = fileExtended;
                    filesAdded++;
                    DisplayCompletionInfo("completion percentage", filesAdded, totalFilesCount);
                }
                catch (Exception ex)
                {
                    var error = new AppError(timeStamp, "InitializeFiles",
                                             filePath,
                                             "the file cannot be read: " + ex.Message);
                    ErrorHandling.WriteErrorLog(confInstance, error);
                    throw ex;
                }
            }

            if (someFilesFailed)
            {
                Console.WriteLine("\nSome files could not be added, please see the error log for details:");
                Console.WriteLine(confInstance.ErrorLogFile);
            }

            return(filesArray.Where(f => f != null).ToDictionary(f => f.fileID, f => f));
        }
Ejemplo n.º 4
0
        public static string ResolvePath(SyncConfig confInstance, string folderPath)
        {
            string resPath = null;
            string volLetter;

            try
            {
                int volLabelendInd = folderPath.IndexOfAny(new char[] { ':', '\\' });

                // if volLabelendInd == 1 it means that volume label is not used
                // and the input path should be returned 'as-is'
                if (volLabelendInd > 1)
                {
                    string volLabel = folderPath.Substring(0, volLabelendInd);



                    DriveInfo[] allDrives = DriveInfo.GetDrives();
                    try
                    {
                        volLetter = allDrives.FirstOrDefault(x => x.VolumeLabel == volLabel).Name;
                    }
                    catch (NullReferenceException ex)
                    {
                        return(null);
                    }

                    resPath = folderPath.Replace(volLabel, volLetter.Substring(0, 1));
                }
                else
                {
                    resPath = folderPath;
                }

                if (!Directory.Exists(resPath))
                {
                    throw new DirectoryNotFoundException();
                }
            }
            catch (Exception e)
            {
                var error = new AppError(DateTime.Now, e.TargetSite.ToString(),
                                         "resolving path " + folderPath, e.Message);
                ErrorHandling.WriteErrorLog(confInstance, error);
                throw e;
            }


            return(resPath);
        }
        public static void HandleException(SyncConfig confInstance, Exception ex)
        {
            var appError = new AppError(ex);

            WriteErrorLog(confInstance, appError);
        }