| | | 1 | | using ArturRios.Logging.Configuration; |
| | | 2 | | using ArturRios.Logging.Factories; |
| | | 3 | | using ArturRios.Logging.Interfaces; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Logging.Loggers; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Appends log entries to files on disk, organised by the folder scheme and split level in the configuration. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Writes are serialized on a process-wide lock, so entries from concurrent threads never interleave. |
| | | 12 | | /// </remarks> |
| | | 13 | | /// <param name="configuration">Controls the application name, base path, folder scheme and split level.</param> |
| | 55 | 14 | | public class FileLogger(FileLoggerConfiguration configuration) : IInternalLogger |
| | | 15 | | { |
| | | 16 | | private const string DefaultLogFolder = "log"; |
| | | 17 | | private const string FileExtension = ".log"; |
| | 1 | 18 | | private static readonly Lock s_fileLock = new(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The folder name used by <see cref="LogFolderScheme.ByRequest"/>, fixed for the lifetime of this |
| | | 22 | | /// logger. Generating it per write put every single entry in a folder of its own. |
| | | 23 | | /// </summary> |
| | 55 | 24 | | private readonly string _instanceFolderName = Guid.NewGuid().ToString(); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public void Trace(string message, string filePath, string methodName) |
| | | 28 | | { |
| | 4 | 29 | | Write(CustomLogLevel.Trace, filePath, methodName, message); |
| | 4 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public void Debug(string message, string filePath, string methodName) |
| | | 34 | | { |
| | 4 | 35 | | Write(CustomLogLevel.Debug, filePath, methodName, message); |
| | 4 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public void Info(string message, string filePath, string methodName) |
| | | 40 | | { |
| | 49 | 41 | | Write(CustomLogLevel.Information, filePath, methodName, message); |
| | 49 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public void Warn(string message, string filePath, string methodName) |
| | | 46 | | { |
| | 4 | 47 | | Write(CustomLogLevel.Warning, filePath, methodName, message); |
| | 4 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public void Error(string message, string filePath, string methodName) |
| | | 52 | | { |
| | 4 | 53 | | Write(CustomLogLevel.Error, filePath, methodName, message); |
| | 4 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public void Exception(string message, string filePath, string methodName) |
| | | 58 | | { |
| | 3 | 59 | | Write(CustomLogLevel.Exception, filePath, methodName, message); |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public void Critical(string message, string filePath, string methodName) |
| | | 64 | | { |
| | 3 | 65 | | Write(CustomLogLevel.Critical, filePath, methodName, message); |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public void Fatal(string message, string filePath, string methodName) |
| | | 70 | | { |
| | 3 | 71 | | Write(CustomLogLevel.Fatal, filePath, methodName, message); |
| | 3 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void Write(CustomLogLevel level, string filePath, string methodName, string message) |
| | | 75 | | { |
| | 74 | 76 | | var path = BuildFullPath(); |
| | | 77 | | |
| | 74 | 78 | | CreateDirectoryIfNotExists(path); |
| | | 79 | | |
| | | 80 | | lock (s_fileLock) |
| | | 81 | | { |
| | 74 | 82 | | File.AppendAllText(path, LogEntryFactory.Create(level, filePath, methodName, message)); |
| | 74 | 83 | | } |
| | 74 | 84 | | } |
| | | 85 | | |
| | | 86 | | private static void CreateDirectoryIfNotExists(string path) |
| | | 87 | | { |
| | 74 | 88 | | var directory = Path.GetDirectoryName(path); |
| | | 89 | | |
| | 74 | 90 | | if (directory is not null && !Directory.Exists(directory)) |
| | | 91 | | { |
| | 51 | 92 | | Directory.CreateDirectory(directory); |
| | | 93 | | } |
| | 74 | 94 | | } |
| | | 95 | | |
| | | 96 | | private string BuildFullPath() |
| | | 97 | | { |
| | 74 | 98 | | var timestamp = DateTime.UtcNow; |
| | 74 | 99 | | var folderPath = BuildFolderPath(timestamp); |
| | 74 | 100 | | var fileName = BuildFileName(timestamp); |
| | | 101 | | |
| | 74 | 102 | | return Path.Combine(folderPath, $"{fileName}{FileExtension}"); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private string BuildFolderPath(DateTime timestamp) |
| | | 106 | | { |
| | 74 | 107 | | var basePath = configuration.FilePath ?? Path.Combine(AppContext.BaseDirectory, DefaultLogFolder); |
| | 74 | 108 | | var path = basePath; |
| | | 109 | | |
| | 74 | 110 | | switch (configuration.FolderScheme) |
| | | 111 | | { |
| | | 112 | | case LogFolderScheme.AllInOne: |
| | | 113 | | break; |
| | | 114 | | case LogFolderScheme.ByYear: |
| | 2 | 115 | | path = Path.Combine(path, timestamp.Year.ToString()); |
| | 2 | 116 | | break; |
| | | 117 | | case LogFolderScheme.ByMonth: |
| | 3 | 118 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2")); |
| | 3 | 119 | | break; |
| | | 120 | | case LogFolderScheme.ByDay: |
| | 2 | 121 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2"), timestamp.Day.ToStr |
| | 2 | 122 | | break; |
| | | 123 | | case LogFolderScheme.ByHour: |
| | 2 | 124 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2"), timestamp.Day.ToStr |
| | 2 | 125 | | break; |
| | | 126 | | case LogFolderScheme.ByRequest: |
| | 7 | 127 | | path = Path.Combine(path, _instanceFolderName); |
| | 7 | 128 | | break; |
| | | 129 | | default: |
| | 0 | 130 | | throw new ArgumentOutOfRangeException( |
| | 0 | 131 | | nameof(configuration), configuration.FolderScheme, "Unknown log folder scheme."); |
| | | 132 | | } |
| | | 133 | | |
| | 74 | 134 | | return path; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | private string BuildFileName(DateTime timestamp) |
| | | 138 | | { |
| | 74 | 139 | | return configuration.FileSplitLevel switch |
| | 74 | 140 | | { |
| | 2 | 141 | | LogSplitLevel.Request => configuration.ApplicationName, |
| | 2 | 142 | | LogSplitLevel.Year => $"{configuration.ApplicationName}_{timestamp:yyyy}", |
| | 2 | 143 | | LogSplitLevel.Month => $"{configuration.ApplicationName}_{timestamp:yyyy_MM}", |
| | 66 | 144 | | LogSplitLevel.Day => $"{configuration.ApplicationName}_{timestamp:yyyy_MM_dd}", |
| | 2 | 145 | | LogSplitLevel.Hour => $"{configuration.ApplicationName}_{timestamp:yyyy_MM_dd_HH}", |
| | 0 | 146 | | _ => throw new ArgumentOutOfRangeException( |
| | 0 | 147 | | nameof(configuration), configuration.FileSplitLevel, "Unknown log split level.") |
| | 74 | 148 | | }; |
| | | 149 | | } |
| | | 150 | | } |