Пример #1
0
 void OpenFile(DateTime now)
 {
     string path;
     DateTime nextCheckpoint;
     _roller.GetLogFilePath(now, out path, out nextCheckpoint);
     _nextCheckpoint = nextCheckpoint;
     _currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes);
     ApplyRetentionPolicy(path);
 }
Пример #2
0
 void CloseFile()
 {
     _currentFile.Dispose();
     _currentFile = null;
     _nextCheckpoint = null;
 }
Пример #3
0
        void OpenFile(DateTime now)
        {
            var date = now.Date;

            // We only take one attempt at it because repeated failures
            // to open log files REALLY slow an app down.
            _nextCheckpoint = date.AddDays(1);

            var existingFiles = Enumerable.Empty<string>();
            try
            {
                existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
                                         .Select(Path.GetFileName);
            }
            catch (DirectoryNotFoundException) { }

            var latestForThisDate = _roller
                .SelectMatches(existingFiles)
                .Where(m => m.Date == date)
                .OrderByDescending(m => m.SequenceNumber)
                .FirstOrDefault();

            var sequence = latestForThisDate != null ? latestForThisDate.SequenceNumber : 0;

            const int maxAttempts = 3;
            for (var attempt = 0; attempt < maxAttempts; attempt++)
            {
                string path;
                _roller.GetLogFilePath(now, sequence, out path);

                try
                {
                    _currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding);
                }
                catch (IOException ex)
                {
                    var errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
                    if (errorCode == 32 || errorCode == 33)
                    {
                        SelfLog.WriteLine("Rolling file target {0} was locked, attempting to open next in sequence (attempt {1})", path, attempt + 1);
                        sequence++;
                        continue;
                    }

                    throw;
                }

                ApplyRetentionPolicy(path);
                return;
            }
        }
        /// <summary>
        /// Write log events to the specified file.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="path">Path to the file.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
        /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
        /// For unrestricted growth, pass null. The default is 1 GB.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <remarks>The file will be written using the UTF-8 character set.</remarks>
        public static LoggerConfiguration File(
            this LoggerSinkConfiguration sinkConfiguration,
            string path,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate = DefaultOutputTemplate,
            IFormatProvider formatProvider = null,
            long? fileSizeLimitBytes = DefaultFileSizeLimitBytes)
        {
            if (sinkConfiguration == null) throw new ArgumentNullException("sinkConfiguration");
            if (outputTemplate == null) throw new ArgumentNullException("outputTemplate");
            var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);

            FileSink sink;
            try
            {
                sink = new FileSink(path, formatter, fileSizeLimitBytes);
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Unable to open file sink for {0}: {1}", path, ex);
                return sinkConfiguration.Sink(new NullSink());
            }

            return sinkConfiguration.Sink(sink, restrictedToMinimumLevel);
        }
Пример #5
0
 void CloseFile()
 {
     _currentFile.Dispose();
     _currentFile = null;
     _limitOfCurrentFile = null;
 }
Пример #6
0
 void OpenFile(DateTimeOffset timeStamp)
 {
     var limit = timeStamp.Date.AddDays(1);
     var path = string.Format(_pathFormat, timeStamp.Date.ToString("yyyy-MM-dd"));
     _currentFile = new FileSink(path, _textFormatter);
     _limitOfCurrentFile = limit;
 }