Revize d7aeccba
Přidáno uživatelem Pultak před asi 2 roky(ů)
ld_client/LDClient/utils/loggers/FileLogger.cs | ||
---|---|---|
4 | 4 |
|
5 | 5 |
public class FileLogger : ALogger { |
6 | 6 |
|
7 |
/// <summary> |
|
8 |
/// Folder name for the created log files |
|
9 |
/// </summary> |
|
7 | 10 |
private const string LogFolderName = "logs"; |
11 |
/// <summary> |
|
12 |
/// Base name of the log file |
|
13 |
/// </summary> |
|
8 | 14 |
private const string LogFileName = "app_info.log"; |
15 |
|
|
9 | 16 |
private readonly int _logChunkSize = Program.Config.LogChunkSize; |
10 | 17 |
private readonly int _logChunkMaxCount = Program.Config.LogChunkMaxCount; |
11 | 18 |
private readonly int _logArchiveMaxCount = Program.Config.LogArchiveMaxCount; |
12 | 19 |
private readonly int _logCleanupPeriod = Program.Config.LogCleanupPeriod; |
13 | 20 |
|
21 |
/// <summary> |
|
22 |
/// Destination folder used to store the created logs and zipped chunks |
|
23 |
/// </summary> |
|
14 | 24 |
private const string LogFolderPath = $"ldClient\\{LogFolderName}"; |
15 | 25 |
|
26 |
/// <summary> |
|
27 |
/// Flag that indicates that the log folder is already created |
|
28 |
/// </summary> |
|
16 | 29 |
private bool _logDirExists; |
17 | 30 |
|
31 |
/// <summary> |
|
32 |
/// Creates one entry in the rotating file. |
|
33 |
/// If the current log file is too big, it creates new log file. |
|
34 |
/// If there is too many log files it archives them. |
|
35 |
/// Deletes all archived files that are too old |
|
36 |
/// </summary> |
|
37 |
/// <param name="message">Desired message to be logged<</param> |
|
18 | 38 |
protected override void CreateLog(string message) { |
19 | 39 |
|
20 | 40 |
if (!_logDirExists) { |
... | ... | |
33 | 53 |
sw.WriteLine(message); |
34 | 54 |
} |
35 | 55 |
|
56 |
/// <summary> |
|
57 |
/// Rotates last log file by creating new logging file in the process |
|
58 |
/// </summary> |
|
59 |
/// <param name="filePath">path to the last log file</param> |
|
36 | 60 |
private void Rotate(string filePath) { |
37 | 61 |
if (!File.Exists(filePath)) { |
38 | 62 |
return; |
... | ... | |
60 | 84 |
DeleteOldArchives(logFolderContent); |
61 | 85 |
} |
62 | 86 |
|
87 |
/// <summary> |
|
88 |
/// Archives the all the last log files (chunks) |
|
89 |
/// </summary> |
|
90 |
/// <param name="chunks">All log files that will be archived</param> |
|
91 |
/// <param name="rotatedPath">path to the log files, which will be archived</param> |
|
92 |
/// <param name="fileTime">current time stamp in string</param> |
|
93 |
/// <param name="folderPath">path to to the exported archives</param> |
|
63 | 94 |
private void Archive(IEnumerable<FileSystemInfo> chunks, string rotatedPath, string fileTime, string? folderPath) { |
64 | 95 |
|
65 | 96 |
var archiveFolderInfo = Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(rotatedPath) ?? LogFolderPath, $"{LogFolderName}_{fileTime}")); |
... | ... | |
73 | 104 |
Directory.Delete(archiveFolderInfo.FullName, true); |
74 | 105 |
} |
75 | 106 |
|
107 |
/// <summary> |
|
108 |
/// This function deletes all archives that are too old and exceeds the maximum zip files. |
|
109 |
/// Cleanup period and zip max count can be specified in the configuration file. |
|
110 |
/// </summary> |
|
111 |
/// <param name="logFolderContent">filesystem info of the log folder</param> |
|
76 | 112 |
private void DeleteOldArchives(FileSystemInfo[] logFolderContent) { |
77 | 113 |
|
78 | 114 |
var archives = logFolderContent.Where(x => x.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase)).ToArray(); |
79 |
|
|
115 |
|
|
80 | 116 |
if (archives.Length <= _logArchiveMaxCount) |
81 | 117 |
return; |
82 | 118 |
|
119 |
//find oldest archive in the folder |
|
83 | 120 |
var oldestArchive = archives.OrderBy(x => x.CreationTime).First(); |
84 | 121 |
var cleanupDate = oldestArchive.CreationTime.AddDays(_logCleanupPeriod); |
122 |
//is there any file older than specified cleanup cleanup period |
|
85 | 123 |
if (DateTime.Compare(cleanupDate, DateTime.Now) <= 0) { |
86 | 124 |
foreach (var file in logFolderContent) { |
87 | 125 |
file.Delete(); |
... | ... | |
90 | 128 |
File.Delete(oldestArchive.FullName); |
91 | 129 |
} |
92 | 130 |
} |
93 |
|
|
94 | 131 |
public override string ToString() => $"{base.ToString()}, Chunk Size: {_logChunkSize}, Max chunk count: {_logChunkMaxCount}, Max log archive count: {_logArchiveMaxCount}, Cleanup period: {_logCleanupPeriod} days]"; |
95 | 132 |
} |
Také k dispozici: Unified diff
re #9570 Added code documentation to logger classes and properties