Exemple #1
0
        private bool TryGetNewStreamAndApplyRetentionPolicies(long maxFileSize, out FileStream fileStream)
        {
            string[] logFiles;
            string[] logGzFiles;
            try
            {
                logFiles   = Directory.GetFiles(_path, "*.log");
                logGzFiles = Directory.GetFiles(_path, "*.log.gz");
            }
            catch (Exception)
            {
                // Something went wrong we will try again later
                fileStream = null;
                return(false);
            }
            Array.Sort(logFiles);
            Array.Sort(logGzFiles);

            if (DateTime.Today != _today)
            {
                _today      = DateTime.Today;
                _dateString = LogInfo.DateToLogFormat(DateTime.Today);
            }

            if (_logNumber < 0)
            {
                _logNumber = Math.Max(LastLogNumberForToday(logFiles), LastLogNumberForToday(logGzFiles));
            }

            UpdateLocalDateTimeOffset();

            string fileName;

            while (true)
            {
                fileName = Path.Combine(_path, LogInfo.GetNewFileName(_dateString, _logNumber));
                if (File.Exists(fileName))
                {
                    if (new FileInfo(fileName).Length < maxFileSize)
                    {
                        break;
                    }
                }
                else if (File.Exists(LogInfo.AddCompressExtension(fileName)) == false)
                {
                    break;
                }
                _logNumber++;
            }

            if (Compressing == false)
            {
                CleanupOldLogFiles(logFiles);
                LimitLogSize(logFiles);
            }

            fileStream = SafeFileStream.Create(fileName, FileMode.Append, FileAccess.Write, FileShare.Read, 32 * 1024, false);
            fileStream.Write(_headerRow, 0, _headerRow.Length);
            return(true);
        }