|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in https://github.com/aspnet/Logging for license information. |
| 3 | +// https://github.com/aspnet/Logging/blob/2d2f31968229eddb57b6ba3d34696ef366a6c71b/src/Microsoft.Extensions.Logging.AzureAppServices/Internal/FileLoggerProvider.cs |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Options; |
| 14 | + |
| 15 | +using OSharp.Logging.RollingFile.Formatters; |
| 16 | +using OSharp.Logging.RollingFile.Internal; |
| 17 | + |
| 18 | + |
| 19 | +namespace OSharp.Logging.RollingFile |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// An <see cref="ILoggerProvider" /> that writes logs to a file |
| 23 | + /// </summary> |
| 24 | + [ProviderAlias("File")] |
| 25 | + public class FileLoggerProvider : BatchingLoggerProvider |
| 26 | + { |
| 27 | + private readonly string _path; |
| 28 | + private readonly string _fileName; |
| 29 | + private readonly string _extension; |
| 30 | + private readonly int? _maxFileSize; |
| 31 | + private readonly int? _maxRetainedFiles; |
| 32 | + private readonly int _maxFileCountPerPeriodicity; |
| 33 | + private readonly PeriodicityOptions _periodicity; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Creates an instance of the <see cref="OSharp.Logging.RollingFile.FileLoggerProvider" /> |
| 37 | + /// </summary> |
| 38 | + /// <param name="options">The options object controlling the logger</param> |
| 39 | + /// <param name="formatter"></param> |
| 40 | + public FileLoggerProvider(IOptionsMonitor<FileLoggerOptions> options, IEnumerable<ILogFormatter> formatter) : base(options, formatter) |
| 41 | + { |
| 42 | + var loggerOptions = options.CurrentValue; |
| 43 | + _path = loggerOptions.LogDirectory; |
| 44 | + _fileName = loggerOptions.FileName; |
| 45 | + _extension = string.IsNullOrEmpty(loggerOptions.Extension) ? null : "." + loggerOptions.Extension; |
| 46 | + _maxFileSize = loggerOptions.FileSizeLimit; |
| 47 | + _maxRetainedFiles = loggerOptions.RetainedFileCountLimit; |
| 48 | + _maxFileCountPerPeriodicity = loggerOptions.FilesPerPeriodicityLimit ?? 1; |
| 49 | + _periodicity = loggerOptions.Periodicity; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /// <inheritdoc /> |
| 54 | + protected override async Task WriteMessagesAsync(IEnumerable<LogMessage> messages, CancellationToken cancellationToken) |
| 55 | + { |
| 56 | + Directory.CreateDirectory(_path); |
| 57 | + |
| 58 | + foreach (var group in messages.GroupBy(GetGrouping)) |
| 59 | + { |
| 60 | + var baseName = GetBaseName(group.Key); |
| 61 | + var fullName = GetLogFilePath(baseName, group.Key); |
| 62 | + |
| 63 | + if (fullName == null) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + using (var streamWriter = File.AppendText(fullName)) |
| 69 | + { |
| 70 | + foreach (var item in group) |
| 71 | + { |
| 72 | + await streamWriter.WriteAsync(item.Message); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + RollFiles(); |
| 78 | + } |
| 79 | + |
| 80 | + private string GetLogFilePath(string baseName, (int Year, int Month, int Day, int Hour, int Minute) fileNameGrouping) |
| 81 | + { |
| 82 | + if (_maxFileCountPerPeriodicity == 1) |
| 83 | + { |
| 84 | + var fullPath = Path.Combine(_path, $"{baseName}{_extension}"); |
| 85 | + return IsAvailable(fullPath) ? fullPath : null; |
| 86 | + } |
| 87 | + |
| 88 | + var counter = GetCurrentCounter(baseName); |
| 89 | + |
| 90 | + while (counter < _maxFileCountPerPeriodicity) |
| 91 | + { |
| 92 | + var fullName = Path.Combine(_path,$"{baseName}.{counter}{_extension}"); |
| 93 | + if (!IsAvailable(fullName)) |
| 94 | + { |
| 95 | + counter++; |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + return fullName; |
| 100 | + } |
| 101 | + |
| 102 | + return null; |
| 103 | + |
| 104 | + bool IsAvailable(string filename) |
| 105 | + { |
| 106 | + var fileInfo = new FileInfo(filename); |
| 107 | + return !(_maxFileSize > 0 && fileInfo.Exists && fileInfo.Length > _maxFileSize); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private int GetCurrentCounter(string baseName) |
| 112 | + { |
| 113 | + try |
| 114 | + { |
| 115 | + var files = Directory.GetFiles(_path, $"{baseName}.*{_extension}"); |
| 116 | + if (files.Length == 0) |
| 117 | + { |
| 118 | + // No rolling file currently exists with the base name as pattern |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + // Get file with highest counter |
| 123 | + var latestFile = files.OrderByDescending(file => file).First(); |
| 124 | + |
| 125 | + var baseNameLength = Path.Combine(_path, baseName).Length + 1; |
| 126 | + var fileWithoutPrefix = latestFile |
| 127 | + .AsSpan() |
| 128 | + .Slice(baseNameLength); |
| 129 | + var indexOfPeriod = fileWithoutPrefix.IndexOf('.'); |
| 130 | + if (indexOfPeriod < 0) |
| 131 | + { |
| 132 | + // No additional dot could be found |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | + var counterSpan = fileWithoutPrefix.Slice(0, indexOfPeriod); |
| 137 | + if (int.TryParse(counterSpan.ToString(), out var counter)) |
| 138 | + { |
| 139 | + return counter; |
| 140 | + } |
| 141 | + |
| 142 | + return 0; |
| 143 | + } |
| 144 | + catch (Exception) |
| 145 | + { |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + private string GetBaseName((int Year, int Month, int Day, int Hour, int Minute) group) |
| 152 | + { |
| 153 | + switch (_periodicity) |
| 154 | + { |
| 155 | + case PeriodicityOptions.Minutely: |
| 156 | + return $"{_fileName}{group.Year:0000}{group.Month:00}{group.Day:00}{group.Hour:00}{group.Minute:00}"; |
| 157 | + case PeriodicityOptions.Hourly: |
| 158 | + return $"{_fileName}{group.Year:0000}{group.Month:00}{group.Day:00}{group.Hour:00}"; |
| 159 | + case PeriodicityOptions.Daily: |
| 160 | + return $"{_fileName}{group.Year:0000}{group.Month:00}{group.Day:00}"; |
| 161 | + case PeriodicityOptions.Monthly: |
| 162 | + return $"{_fileName}{group.Year:0000}{group.Month:00}"; |
| 163 | + } |
| 164 | + throw new InvalidDataException("Invalid periodicity"); |
| 165 | + } |
| 166 | + |
| 167 | + private (int Year, int Month, int Day, int Hour, int Minute) GetGrouping(LogMessage message) |
| 168 | + { |
| 169 | + return (message.Timestamp.Year, message.Timestamp.Month, message.Timestamp.Day, message.Timestamp.Hour, message.Timestamp.Minute); |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Deletes old log files, keeping a number of files defined by <see cref="FileLoggerOptions.RetainedFileCountLimit" /> |
| 174 | + /// </summary> |
| 175 | + protected void RollFiles() |
| 176 | + { |
| 177 | + if (_maxRetainedFiles > 0) |
| 178 | + { |
| 179 | + var groupsToDelete = new DirectoryInfo(_path) |
| 180 | + .GetFiles(_fileName + "*") |
| 181 | + .GroupBy(file => GetFilenameForGrouping(file.Name)) |
| 182 | + .OrderByDescending(f => f.Key) |
| 183 | + .Skip(_maxRetainedFiles.Value); |
| 184 | + |
| 185 | + foreach (var groupToDelete in groupsToDelete) |
| 186 | + { |
| 187 | + foreach (var fileToDelete in groupToDelete) |
| 188 | + { |
| 189 | + fileToDelete.Delete(); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + string GetFilenameForGrouping(string filename) |
| 195 | + { |
| 196 | + var hasExtension = !string.IsNullOrEmpty(_extension); |
| 197 | + var isMultiFile = _maxFileCountPerPeriodicity > 1; |
| 198 | + if (!hasExtension && !isMultiFile) |
| 199 | + return filename; |
| 200 | + |
| 201 | + if(!isMultiFile || !hasExtension) |
| 202 | + return Path.GetFileNameWithoutExtension(filename); |
| 203 | + |
| 204 | + return Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(filename)); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments