Exemple #1
0
        private void HandleError(FileWrapper fileWrapper, string reason, Exception exception = null)
        {
            MoveFileToErrorDirectory(fileWrapper.FileName);
            var fileName = Path.GetFileName(fileWrapper.FileName + ErrorReasonFileExtension);
            //// ReSharper disable AssignNullToNotNullAttribute
            // ReSharper disable AssignNullToNotNullAttribute
            var path = Path.Combine(_errorDirectory, fileName);
            // ReSharper restore AssignNullToNotNullAttribute
            //// ReSharper restore AssignNullToNotNullAttribute
            if (exception == null)
            {
                Logger.Log(NotificationSeverity.Error, reason);
                File.WriteAllText(path, reason);
            }
            else
            {
                Logger.Log(NotificationSeverity.Error, reason, exception);
                File.WriteAllText(path, reason + Environment.NewLine + exception);
            }

            fileWrapper.Processed = true;
        }
Exemple #2
0
 private void LoadFile(ICollection<FileWrapper> files, FileSystemInfo file)
 {
     try
     {
         var fileWrapper = new FileWrapper
                           {
                               FileName = file.FullName,
                               FileDate = file.LastWriteTime
                           };
         files.Add(fileWrapper);
     }
     catch (IOException ioException)
     {
         _directoryChanged = true;
         Logger.Log(NotificationSeverity.Warning, "=> file " + file + " is in use... ", ioException);
     }
 }
Exemple #3
0
        public void ProcessFile(FileWrapper fileWrapper)
        {
            try
            {
                if (!_fileSystemService.FileExists(fileWrapper.FileName))
                {
                    fileWrapper.Processed = true;
                    return; // was already handled
                }

                var handlers = _fileHandlers.Where(x => x.CanHandle(fileWrapper)).ToList();
                if (!handlers.Any())
                {
                    HandleError(fileWrapper, "=> Don't have a handler for " + fileWrapper.FileName);
                    return;
                }
                if (handlers.Count > 1)
                {
                    HandleError(fileWrapper, "=> Found multiple handlers for " + fileWrapper.FileName);
                    return;
                }
                var deleteFile = handlers.Single().Handle(fileWrapper);
                if (deleteFile)
                {
            // ReSharper disable AssignNullToNotNullAttribute
                    var newFile = Path.Combine(_archiveDirectory, Path.GetFileName(fileWrapper.FileName));
            // ReSharper restore AssignNullToNotNullAttribute
                    if (_fileSystemService.FileExists(newFile))
                    {
                        _fileSystemService.DeleteFile(newFile);
                    }
                    _fileSystemService.MoveFile(fileWrapper.FileName, newFile);
                }
                else
                {
                    HandleError(fileWrapper, "=> Could not process " + fileWrapper.FileName);
                }
                fileWrapper.Processed = true;
            }
            catch (InvalidOperationException e)
            {
                HandleError(fileWrapper, "=> Error while processing " + fileWrapper.FileName, e);
            }
            catch (Exception e)
            {
                if (e.GetType().Name.Contains("LazyInitializationException"))
                {
                    if (!e.Message.EndsWith("Could not initialize proxy - no Session."))
                    {
                        HandleError(fileWrapper, "=> Error while processing " + fileWrapper.FileName, e);
                    }
                }
                else
                {
                    HandleError(fileWrapper, "=> Error while processing " + fileWrapper.FileName, e);
                }
            }
        }