1 |
74bd1e40
|
Pultak
|
using System.IO.Compression;
|
2 |
|
|
|
3 |
|
|
namespace LDClient.utils.loggers;
|
4 |
|
|
|
5 |
|
|
public class FileLogger : ALogger {
|
6 |
|
|
|
7 |
|
|
private const string LogFolderName = "logs";
|
8 |
|
|
private const string LogFileName = "app_info.log";
|
9 |
|
|
private readonly int _logChunkSize = Program.Config.LogChunkSize;
|
10 |
|
|
private readonly int _logChunkMaxCount = Program.Config.LogChunkMaxCount;
|
11 |
|
|
private readonly int _logArchiveMaxCount = Program.Config.LogArchiveMaxCount;
|
12 |
|
|
private readonly int _logCleanupPeriod = Program.Config.LogCleanupPeriod;
|
13 |
|
|
|
14 |
|
|
private const string LogFolderPath = $"ldClient\\{LogFolderName}";
|
15 |
|
|
|
16 |
|
|
private bool _logDirExists;
|
17 |
|
|
|
18 |
|
|
protected override void CreateLog(string message) {
|
19 |
|
|
|
20 |
|
|
if (!_logDirExists) {
|
21 |
|
|
_logDirExists = Directory.Exists(LogFolderPath);
|
22 |
|
|
if (!_logDirExists) {
|
23 |
|
|
Directory.CreateDirectory(LogFolderPath);
|
24 |
|
|
_logDirExists = true;
|
25 |
|
|
}
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
var logFilePath = Path.Combine(LogFolderPath, LogFileName);
|
29 |
|
|
|
30 |
|
|
Rotate(logFilePath);
|
31 |
|
|
|
32 |
|
|
using var sw = File.AppendText(logFilePath);
|
33 |
|
|
sw.WriteLine(message);
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
private void Rotate(string filePath) {
|
37 |
|
|
if (!File.Exists(filePath)) {
|
38 |
|
|
return;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
var fileInfo = new FileInfo(filePath);
|
42 |
|
|
if (fileInfo.Length < _logChunkSize) {
|
43 |
|
|
return;
|
44 |
|
|
}
|
45 |
|
|
var fileTime = DateTime.Now.ToString("dd-MM-yyyy,hh-mm-ss,fff");
|
46 |
|
|
var rotatedPath = filePath.Replace(".log", $".{fileTime}");
|
47 |
|
|
File.Move(filePath, rotatedPath);
|
48 |
|
|
|
49 |
|
|
var folderPath = Path.GetDirectoryName(rotatedPath);
|
50 |
|
|
var logFolderContent = new DirectoryInfo(folderPath ?? string.Empty).GetFileSystemInfos();
|
51 |
|
|
|
52 |
|
|
var chunks = logFolderContent.Where(x =>
|
53 |
|
|
!x.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase));
|
54 |
|
|
|
55 |
|
|
if (chunks.Count() <= _logChunkMaxCount) {
|
56 |
|
|
return;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
Archive(chunks, rotatedPath, fileTime, folderPath);
|
60 |
|
|
DeleteOldArchives(logFolderContent);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
private void Archive(IEnumerable<FileSystemInfo> chunks, string rotatedPath, string fileTime, string? folderPath) {
|
64 |
|
|
|
65 |
|
|
var archiveFolderInfo = Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(rotatedPath) ?? LogFolderPath, $"{LogFolderName}_{fileTime}"));
|
66 |
|
|
|
67 |
|
|
foreach (var chunk in chunks) {
|
68 |
|
|
var destination = Path.Combine(archiveFolderInfo.FullName, chunk.Name);
|
69 |
|
|
Directory.Move(chunk.FullName, destination);
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
ZipFile.CreateFromDirectory(archiveFolderInfo.FullName, Path.Combine(folderPath ?? LogFolderPath, $"{LogFolderName}_{fileTime}.zip"));
|
73 |
|
|
Directory.Delete(archiveFolderInfo.FullName, true);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
private void DeleteOldArchives(FileSystemInfo[] logFolderContent) {
|
77 |
|
|
|
78 |
|
|
var archives = logFolderContent.Where(x => x.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase)).ToArray();
|
79 |
|
|
|
80 |
|
|
if (archives.Length <= _logArchiveMaxCount)
|
81 |
|
|
return;
|
82 |
|
|
|
83 |
|
|
var oldestArchive = archives.OrderBy(x => x.CreationTime).First();
|
84 |
|
|
var cleanupDate = oldestArchive.CreationTime.AddDays(_logCleanupPeriod);
|
85 |
|
|
if (DateTime.Compare(cleanupDate, DateTime.Now) <= 0) {
|
86 |
|
|
foreach (var file in logFolderContent) {
|
87 |
|
|
file.Delete();
|
88 |
|
|
}
|
89 |
|
|
} else {
|
90 |
|
|
File.Delete(oldestArchive.FullName);
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
public override string ToString() => $"{base.ToString()}, Chunk Size: {_logChunkSize}, Max chunk count: {_logChunkMaxCount}, Max log archive count: {_logArchiveMaxCount}, Cleanup period: {_logCleanupPeriod} days]";
|
95 |
|
|
}
|