forked from ClassIsland/ClassIsland
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
|
||
namespace ClassIsland.Helpers; | ||
|
||
public static class GZipHelper | ||
{ | ||
public static void CompressFileAndDelete(string path) | ||
{ | ||
using var originalFileStream = File.Open(path, FileMode.Open); | ||
using var compressedFileStream = File.Create(path + ".gz"); | ||
using var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress); | ||
originalFileStream.CopyTo(compressor); | ||
compressor.Close(); | ||
originalFileStream.Close(); | ||
File.Delete(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Windows.Forms; | ||
using Microsoft.Extensions.Logging; | ||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; | ||
|
||
namespace ClassIsland.Services.Logging; | ||
|
||
public class FileLogger(FileLoggerProvider provider, string categoryName) : ILogger | ||
{ | ||
private FileLoggerProvider Provider { get; } = provider; | ||
private string CategoryName { get; } = categoryName; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
var message = formatter(state, exception) + (exception != null ? "\n" + exception : ""); | ||
Provider.WriteLog($"{DateTime.Now}|{logLevel}|{CategoryName}|{message}"); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return false; | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Windows.Shapes; | ||
using ClassIsland.Helpers; | ||
using Microsoft.Extensions.Logging; | ||
using Path = System.IO.Path; | ||
|
||
namespace ClassIsland.Services.Logging; | ||
|
||
public class FileLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly Stream? _logStream; | ||
private readonly StreamWriter? _logWriter; | ||
|
||
private readonly ConcurrentDictionary<string, FileLogger> _loggers = new(); | ||
|
||
private const int LogRetentionDays = 30; | ||
|
||
private readonly object _lock = new object(); | ||
|
||
public static string GetLogFileName() | ||
{ | ||
var n = 1; | ||
var logs = GetLogs(); | ||
string filename; | ||
do | ||
{ | ||
filename = $"log-{DateTime.Now:yyyy-M-d-HH-mm-ss}-{n}.log"; | ||
n++; | ||
} while (logs.Contains(filename)); | ||
|
||
return filename; | ||
} | ||
|
||
public FileLoggerProvider() | ||
{ | ||
try | ||
{ | ||
var logs = Directory.GetFiles(App.AppLogFolderPath); | ||
var currentLogFile = GetLogFileName(); | ||
_logStream = File.Open(Path.Combine(App.AppLogFolderPath, currentLogFile), FileMode.Create, FileAccess.ReadWrite, FileShare.Read); | ||
_logWriter = new StreamWriter(_logStream) | ||
{ | ||
AutoFlush = true | ||
}; | ||
_ = Task.Run(() => ProcessPreviousLogs(logs, currentLogFile)); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
} | ||
|
||
private static void ProcessPreviousLogs(string[] logs, string currentLogFile) | ||
{ | ||
foreach (var i in logs.Where(x => Path.GetFileName(x) != currentLogFile && Path.GetExtension(x) == ".log")) | ||
{ | ||
try | ||
{ | ||
GZipHelper.CompressFileAndDelete(i); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
} | ||
|
||
var now = DateTime.Now; | ||
foreach (var i in logs.Where(x => (now - File.GetLastWriteTime(x)).TotalDays > LogRetentionDays && | ||
Path.GetFileName(x) != currentLogFile && | ||
(x.EndsWith(".log") || x.EndsWith(".log.gz")))) | ||
{ | ||
try | ||
{ | ||
File.Delete(i); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
} | ||
} | ||
|
||
private static List<string?> GetLogs() | ||
{ | ||
return Directory.GetFiles(App.AppLogFolderPath).Select(Path.GetFileName).ToList(); | ||
} | ||
|
||
internal void WriteLog(string log) | ||
{ | ||
lock (_lock) | ||
{ | ||
_logWriter?.WriteLine(log); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_logWriter?.Close(); | ||
_loggers.Clear(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return _loggers.GetOrAdd(categoryName, new FileLogger(this, categoryName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters