Пример #1
0
        private LogFile?CheckExistingLogFileForCutoverAndSize(
            string applicationName,
            string groupName,
            Func <FileLoggerOptions> options,
            string logFileNamePattern,
            int?logFileMaxSizeInKilobytes,
            LogFileManagementSchedule managementSchedule,
            string fileName,
            LogFile existingLogFile)
        {
            DateTime UtcNow = _SystemTime.UtcNow;

            if (UtcNow >= existingLogFile.ManagementSchedule.NextCutoverTimeUtc)
            {
                string NewFileName = FileNameGenerator.GenerateFileName(applicationName, _SystemTime, groupName, logFileNamePattern !);

                LogFile?NewLogFile = CreateNewLogFile(options().LogFileDirectory !, logFileMaxSizeInKilobytes, managementSchedule, NewFileName, 0);
                if (NewLogFile == null)
                {
                    return(existingLogFile);
                }

                existingLogFile.Stream.Dispose();
                _LogFiles.Remove(fileName);
                if (UtcNow > existingLogFile.ManagementSchedule.NextArchiveTimeUtc)
                {
                    TryArchiveLogFile(options(), existingLogFile.FinalFullPath);
                }

                _LogFiles[NewFileName] = NewLogFile;
                _GroupNameToFileNameCache ![groupName] = NewFileName;
Пример #2
0
        public LogFile?FindLogFile(
            string applicationName,
            string groupName,
            Func <FileLoggerOptions> options,
            string logFileNamePattern,
            int?logFileMaxSizeInKilobytes,
            LogFileManagementSchedule managementSchedule)
        {
            string FileName = FindFileNameForMessage(applicationName, groupName, logFileNamePattern);

            if (_LogFiles.TryGetValue(FileName, out LogFile ExistingLogFile))
            {
                return(CheckExistingLogFileForCutoverAndSize(
                           applicationName,
                           groupName,
                           options,
                           logFileNamePattern,
                           logFileMaxSizeInKilobytes,
                           managementSchedule,
                           FileName,
                           ExistingLogFile));
            }

            LogFile?NewLogFile = CreateNewLogFile(options().LogFileDirectory !, logFileMaxSizeInKilobytes, managementSchedule, FileName, 0);

            if (NewLogFile != null)
            {
                _LogFiles[FileName] = NewLogFile;
            }
            return(NewLogFile);
        }
Пример #3
0
 public LogFile(DateTime createdAtUtc, LogFileManagementSchedule managementSchedule, string fileName, int index, string finalFileName, string finalFullPath, Stream stream)
 {
     CreatedAtUtc       = createdAtUtc;
     ManagementSchedule = managementSchedule;
     FileName           = fileName;
     Index         = index;
     FinalFileName = finalFileName;
     FinalFullPath = finalFullPath;
     Stream        = stream;
 }
Пример #4
0
        private void LogMessageProcessingThreadBody(object?state)
        {
            WaitHandle[] Handles = new WaitHandle[] { _StopHandle, _ArchiveNowHandle, _MessageReadyHandle };

            while (true)
            {
                int HandleIndex = WaitHandle.WaitAny(Handles);
                if (HandleIndex == 0)
                {
                    break;
                }
                if (HandleIndex == 1)
                {
                    _Timer?.Dispose();

                    FileLoggerOptions Options = _Options.CurrentValue;

                    if (_Timer != null || Options.ArchiveLogFilesOnStartup)
                    {
                        _LogFileManager.ArchiveLogFiles(_ApplicationName !, Options, _LogFileNamePattern !);
                    }

                    _ManagementSchedule = LogFileManagementSchedule.Build(s_DefaultSystemTime, Options);

                    _Timer = new Timer(
                        (s) => _ArchiveNowHandle.Set(),
                        state: null,
                        _ManagementSchedule.TimeUntilNextArchiveUtc,
                        TimeSpan.FromMilliseconds(-1));
                    _ArchiveNowHandle.Reset();
                    continue;
                }

                DrainMessages(archiveLogFiles: true);
            }

            // When exiting make sure anything remaining in the queue is pumped to files.
            DrainMessages(archiveLogFiles: false);
        }