| | | 1 | | using ArturRios.Logging.Configuration; |
| | | 2 | | using ArturRios.Logging.Factories; |
| | | 3 | | using ArturRios.Logging.Interfaces; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Logging.Loggers; |
| | | 6 | | |
| | 40 | 7 | | public class FileLogger(FileLoggerConfiguration configuration) : IInternalLogger |
| | | 8 | | { |
| | | 9 | | private const string DefaultLogFolder = "log"; |
| | | 10 | | private const string FileExtension = ".log"; |
| | 1 | 11 | | private static readonly Lock s_fileLock = new(); |
| | | 12 | | |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public void Trace(string message, string filePath, string methodName) |
| | 3 | 15 | | { |
| | 3 | 16 | | Write(CustomLogLevel.Trace, filePath, methodName, message); |
| | 3 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public void Debug(string message, string filePath, string methodName) |
| | 3 | 21 | | { |
| | 3 | 22 | | Write(CustomLogLevel.Debug, filePath, methodName, message); |
| | 3 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public void Info(string message, string filePath, string methodName) |
| | 33 | 27 | | { |
| | 33 | 28 | | Write(CustomLogLevel.Information, filePath, methodName, message); |
| | 33 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public void Warn(string message, string filePath, string methodName) |
| | 2 | 33 | | { |
| | 2 | 34 | | Write(CustomLogLevel.Warning, filePath, methodName, message); |
| | 2 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public void Error(string message, string filePath, string methodName) |
| | 3 | 39 | | { |
| | 3 | 40 | | Write(CustomLogLevel.Error, filePath, methodName, message); |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public void Exception(string message, string filePath, string methodName) |
| | 2 | 45 | | { |
| | 2 | 46 | | Write(CustomLogLevel.Exception, filePath, methodName, message); |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public void Critical(string message, string filePath, string methodName) |
| | 2 | 51 | | { |
| | 2 | 52 | | Write(CustomLogLevel.Critical, filePath, methodName, message); |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public void Fatal(string message, string filePath, string methodName) |
| | 2 | 57 | | { |
| | 2 | 58 | | Write(CustomLogLevel.Fatal, filePath, methodName, message); |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void Write(CustomLogLevel level, string filePath, string methodName, string message) |
| | 50 | 62 | | { |
| | 50 | 63 | | var path = BuildFullPath(); |
| | | 64 | | |
| | 50 | 65 | | CreateDirectoryIfNotExists(path); |
| | | 66 | | |
| | | 67 | | lock (s_fileLock) |
| | 50 | 68 | | { |
| | 50 | 69 | | File.AppendAllText(path, LogEntryFactory.Create(level, filePath, methodName, message)); |
| | 50 | 70 | | } |
| | 50 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static void CreateDirectoryIfNotExists(string path) |
| | 50 | 74 | | { |
| | 50 | 75 | | var directory = Path.GetDirectoryName(path); |
| | | 76 | | |
| | 50 | 77 | | if (directory is not null && !Directory.Exists(directory)) |
| | 46 | 78 | | { |
| | 46 | 79 | | Directory.CreateDirectory(directory); |
| | 46 | 80 | | } |
| | 50 | 81 | | } |
| | | 82 | | |
| | | 83 | | private string BuildFullPath() |
| | 50 | 84 | | { |
| | 50 | 85 | | var timestamp = DateTime.UtcNow; |
| | 50 | 86 | | var folderPath = BuildFolderPath(timestamp); |
| | 50 | 87 | | var fileName = BuildFileName(timestamp); |
| | | 88 | | |
| | 50 | 89 | | return Path.Combine(folderPath, $"{fileName}{FileExtension}"); |
| | 50 | 90 | | } |
| | | 91 | | |
| | | 92 | | private string BuildFolderPath(DateTime timestamp) |
| | 50 | 93 | | { |
| | 50 | 94 | | var basePath = configuration.FilePath ?? Path.Combine(AppContext.BaseDirectory, DefaultLogFolder); |
| | 50 | 95 | | var path = basePath; |
| | | 96 | | |
| | 50 | 97 | | switch (configuration.FolderScheme) |
| | | 98 | | { |
| | | 99 | | case LogFolderScheme.AllInOne: |
| | 44 | 100 | | break; |
| | | 101 | | case LogFolderScheme.ByYear: |
| | 1 | 102 | | path = Path.Combine(path, timestamp.Year.ToString()); |
| | 1 | 103 | | break; |
| | | 104 | | case LogFolderScheme.ByMonth: |
| | 1 | 105 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2")); |
| | 1 | 106 | | break; |
| | | 107 | | case LogFolderScheme.ByDay: |
| | 1 | 108 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2"), timestamp.Day.ToStr |
| | 1 | 109 | | break; |
| | | 110 | | case LogFolderScheme.ByHour: |
| | 1 | 111 | | path = Path.Combine(path, timestamp.Year.ToString(), timestamp.Month.ToString("D2"), timestamp.Day.ToStr |
| | 1 | 112 | | break; |
| | | 113 | | case LogFolderScheme.ByRequest: |
| | 2 | 114 | | var requestId = Guid.NewGuid().ToString(); |
| | 2 | 115 | | path = Path.Combine(path, requestId); |
| | 2 | 116 | | break; |
| | | 117 | | default: |
| | 0 | 118 | | throw new ArgumentOutOfRangeException(configuration.FolderScheme.ToString()); |
| | | 119 | | } |
| | | 120 | | |
| | 50 | 121 | | return path; |
| | 50 | 122 | | } |
| | | 123 | | |
| | | 124 | | private string BuildFileName(DateTime timestamp) |
| | 50 | 125 | | { |
| | 50 | 126 | | return configuration.FileSplitLevel switch |
| | 50 | 127 | | { |
| | 1 | 128 | | LogSplitLevel.Request => configuration.ApplicationName, |
| | 1 | 129 | | LogSplitLevel.Year => $"{configuration.ApplicationName}_{timestamp:yyyy}", |
| | 1 | 130 | | LogSplitLevel.Month => $"{configuration.ApplicationName}_{timestamp:yyyy_MM}", |
| | 46 | 131 | | LogSplitLevel.Day => $"{configuration.ApplicationName}_{timestamp:yyyy_MM_dd}", |
| | 1 | 132 | | LogSplitLevel.Hour => $"{configuration.ApplicationName}_{timestamp:yyyy_MM_dd_HH}", |
| | 0 | 133 | | _ => throw new ArgumentOutOfRangeException(configuration.FileSplitLevel.ToString()) |
| | 50 | 134 | | }; |
| | 50 | 135 | | } |
| | | 136 | | } |