Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MutexMultiProcessFileAppender" /> class.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="parameters">The parameters.</param>
        public MutexMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
        {
            try
            {
                _mutex      = CreateSharableMutex("FileLock");
                _fileStream = CreateFileStream(true);
                _fileCharacteristicsHelper = FileCharacteristicsHelper.CreateHelper(parameters.ForceManaged);
            }
            catch
            {
                if (_mutex != null)
                {
                    _mutex.Close();
                    _mutex = null;
                }

                if (_fileStream != null)
                {
                    _fileStream.Close();
                    _fileStream = null;
                }

                throw;
            }
        }
        protected override DateAndSequenceArchive GenerateArchiveFileInfo(FileInfo archiveFile, FileNameTemplate fileTemplate)
        {
            int sequenceNumber  = ExtractArchiveNumberFromFileName(archiveFile.FullName);
            var creationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(archiveFile, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;

            return(new DateAndSequenceArchive(archiveFile.FullName, creationTimeUtc, string.Empty, sequenceNumber > 0 ? sequenceNumber : 0));
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MutexMultiProcessFileAppender" /> class.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="parameters">The parameters.</param>
        public MutexMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
        {
            try
            {
                this.mutex = CreateSharableMutex("FileLock");
                this.fileStream = CreateFileStream(true);
                this.fileCharacteristicsHelper = FileCharacteristicsHelper.CreateHelper(parameters.ForceManaged);
            }
            catch
            {
                if (this.mutex != null)
                {
                    this.mutex.Close();
                    this.mutex = null;
                }

                if (this.fileStream != null)
                {
                    this.fileStream.Close();
                    this.fileStream = null;
                }

                throw;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowsMultiProcessFileAppender" /> class.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="parameters">The parameters.</param>
 public WindowsMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
 {
     try
     {
         CreateAppendOnlyFile(fileName);
         fileCharacteristicsHelper = FileCharacteristicsHelper.CreateHelper(parameters.ForceManaged);
     }
     catch
     {
         if (fileStream != null)
             fileStream.Dispose();
         fileStream = null;
         throw;
     }
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowsMultiProcessFileAppender" /> class.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="parameters">The parameters.</param>
 public WindowsMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
 {
     try
     {
         CreateAppendOnlyFile(fileName);
         _fileCharacteristicsHelper = FileCharacteristicsHelper.CreateHelper(parameters.ForceManaged);
     }
     catch
     {
         if (_fileStream != null)
         {
             _fileStream.Dispose();
         }
         _fileStream = null;
         throw;
     }
 }
Пример #6
0
        private void UpdateCreationTime()
        {
            FileInfo fileInfo = new FileInfo(this.FileName);

            if (fileInfo.Exists)
            {
                this.CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;
            }
            else
            {
                File.Create(this.FileName).Dispose();
                this.CreationTimeUtc = DateTime.UtcNow;

#if !SILVERLIGHT
                // Set the file's creation time to avoid being thwarted by Windows' Tunneling capabilities (https://support.microsoft.com/en-us/kb/172190).
                File.SetCreationTimeUtc(this.FileName, this.CreationTimeUtc);
#endif
            }
        }
Пример #7
0
        public DateTime?GetFileCreationTimeSource(string filePath, bool fallback)
        {
            var      appender = GetAppender(filePath);
            DateTime?result   = null;

            if (appender != null)
            {
                try
                {
                    result = FileCharacteristicsHelper.ValidateFileCreationTime(appender, (f) => f.GetFileCreationTimeUtc(), (f) => f.CreationTimeUtc, (f) => f.GetFileLastWriteTimeUtc());
                    if (result.HasValue)
                    {
                        // Check if cached value is still valid, and update if not (Will automatically update CreationTimeSource)
                        DateTime cachedTimeUtc = appender.CreationTimeUtc;
                        if (result.Value != cachedTimeUtc)
                        {
                            appender.CreationTimeUtc = result.Value;
                        }
                        return(appender.CreationTimeSource);
                    }
                }
                catch (Exception ex)
                {
                    InternalLogger.Error(ex, "Failed to get file creation time for file '{0}'.", appender.FileName);
                    InvalidateAppender(appender.FileName);
                    throw;
                }
            }
            if (result == null && fallback)
            {
                var fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    result = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;
                    return(Time.TimeSource.Current.FromSystemTime(result.Value));
                }
            }

            return(result);
        }
Пример #8
0
        protected override DateAndSequenceArchive GenerateArchiveFileInfo(FileInfo archiveFile, FileNameTemplate fileTemplate)
        {
            if (fileTemplate?.EndAt > 0)
            {
                string filename    = archiveFile.Name;
                int    templatePos = 0;
                for (int i = 0; i < filename.Length; ++i)
                {
                    char fileNameChar = filename[i];

                    if (templatePos >= fileTemplate.Template.Length)
                    {
                        if (char.IsLetter(fileNameChar))
                        {
                            return(null);    // reached end of template, but still letters
                        }
                        break;
                    }

                    char templateChar;
                    if (templatePos < fileTemplate.EndAt && i >= fileTemplate.BeginAt)
                    {
                        // Inside wildcard, skip validation of non-letters
                        if (char.IsLetter(fileNameChar))
                        {
                            templatePos = fileTemplate.EndAt;
                            do
                            {
                                if (templatePos >= fileTemplate.Template.Length)
                                {
                                    return(null);    // reached end of template, but still letters
                                }
                                templateChar = fileTemplate.Template[templatePos];
                                ++templatePos;
                            } while (!char.IsLetter(templateChar));
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        templateChar = fileTemplate.Template[templatePos];
                        ++templatePos;
                    }

                    if (fileNameChar == templateChar || char.ToLowerInvariant(fileNameChar) == char.ToLowerInvariant(templateChar))
                    {
                        continue;
                    }

                    if (templateChar == '*' && !char.IsLetter(fileNameChar))
                    {
                        break;  // Reached archive-seq-no, lets call it a day
                    }

                    return(null); // filename is not matching file-template
                }
            }

            int sequenceNumber = ExtractArchiveNumberFromFileName(archiveFile.FullName);

            InternalLogger.Trace("FileTarget: extracted sequenceNumber: {0} from file '{1}'", sequenceNumber, archiveFile.FullName);
            var creationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(archiveFile, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;

            return(new DateAndSequenceArchive(archiveFile.FullName, creationTimeUtc, string.Empty, sequenceNumber > 0 ? sequenceNumber : 0));
        }
        public UnixMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
        {
            UnixFileInfo fileInfo   = null;
            bool         fileExists = false;

            try
            {
                fileInfo   = new UnixFileInfo(fileName);
                fileExists = fileInfo.Exists;
            }
            catch
            {
            }

            int fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6)));

            if (fd == -1)
            {
                if (Stdlib.GetLastError() == Errno.ENOENT && parameters.CreateDirs)
                {
                    string dirName = Path.GetDirectoryName(fileName);
                    if (!Directory.Exists(dirName) && parameters.CreateDirs)
                    {
                        Directory.CreateDirectory(dirName);
                    }

                    fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6)));
                }
            }
            if (fd == -1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }

            try
            {
                _file = new UnixStream(fd, true);

                long filePosition = _file.Position;
                if (fileExists || filePosition > 0)
                {
                    if (fileInfo != null)
                    {
                        CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => File.GetCreationTimeUtc(f.FullName), (f) => { f.Refresh(); return(f.LastStatusChangeTimeUtc); }, (f) => DateTime.UtcNow).Value;
                    }
                    else
                    {
                        CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileName, (f) => File.GetCreationTimeUtc(f), (f) => DateTime.UtcNow).Value;
                    }
                }
                else
                {
                    // We actually created the file and eventually concurrent processes
                    CreationTimeUtc = DateTime.UtcNow;
                    File.SetCreationTimeUtc(FileName, CreationTimeUtc);
                }
            }
            catch
            {
                Syscall.close(fd);
                throw;
            }
        }