This commit is contained in:
2024-05-27 20:11:11 +02:00
parent c997b6c1a2
commit 005d91c199
19 changed files with 717 additions and 435 deletions

View File

@@ -5,29 +5,52 @@ using EonaCat.Logger.Managers;
namespace EonaCat.Logger.Test.Web;
public static class Logger
public class Logger
{
private static LogManager LogManager;
public static ELogType MaxLogType { get; set; }
public static bool UseLocalTime { get; set; }
public static string LogFolder => Path.Combine(FileLoggerOptions.DefaultPath, "logs");
public static string CurrentLogFile => LogManager.CurrentLogFile;
public static bool IsDisabled { get; set; }
private LogManager _logManager;
public ELogType MaxLogType { get; set; }
public LoggerSettings LoggerSettings { get; }
public bool UseLocalTime { get; set; }
public string LogFolder { get; set; } = Path.Combine(FileLoggerOptions.DefaultPath, "logs");
public string CurrentLogFile => _logManager.CurrentLogFile;
public bool IsDisabled { get; set; }
public static void DeleteCurrentLogFile()
public Logger(string name = "EonaCatTestLogger", ELogType maxLogType = ELogType.INFO, bool useLocalTime = false, int maxFileSize = 20_000_000)
{
if (IsDisabled)
return;
UseLocalTime = useLocalTime;
MaxLogType = maxLogType;
LogManager.DeleteCurrentLogFile();
LoggerSettings = new LoggerSettings
{
Id = name,
MaxLogType = MaxLogType,
UseLocalTime = UseLocalTime,
FileLoggerOptions =
{
LogDirectory = LogFolder,
FileSizeLimit = maxFileSize,
UseLocalTime = UseLocalTime,
},
};
_logManager = new LogManager(LoggerSettings);
}
private static string ConvertToAbsolutePath(string path)
public void DeleteCurrentLogFile()
{
if (IsDisabled)
{
return;
}
_logManager.DeleteCurrentLogFile();
}
private string ConvertToAbsolutePath(string path)
{
return Path.IsPathRooted(path) ? path : Path.Combine(LogFolder, path);
}
public static async Task DownloadLogAsync(HttpContext context, string logName, long limit)
public async Task DownloadLogAsync(HttpContext context, string logName, long limit)
{
var logFileName = logName + ".log";
@@ -39,7 +62,9 @@ public static class Logger
response.Headers.ContentDisposition = "attachment;filename=" + logFileName;
if (limit > fS.Length || limit < 1)
{
limit = fS.Length;
}
var oFS = new OffsetStream(fS, 0, limit);
var request = context.Request;
@@ -81,43 +106,32 @@ public static class Logger
await oFS.CopyToAsync(stream).ConfigureAwait(false);
if (fS.Length > limit)
{
await stream.WriteAsync("\r\n####__EONACATLOGGER_TRUNCATED___####"u8.ToArray()).ConfigureAwait(false);
}
}
}
public static async Task LogAsync(string message, ELogType logType = ELogType.INFO, bool writeToConsole = true)
public async Task LogAsync(string message, ELogType logType = ELogType.INFO, bool writeToConsole = true)
{
if (IsDisabled)
{
return;
}
await LogManager.WriteAsync(message, logType, writeToConsole).ConfigureAwait(false);
await _logManager.WriteAsync(message, logType, writeToConsole).ConfigureAwait(false);
}
public static async Task LogAsync(Exception exception, string message = "", bool writeToConsole = true)
public async Task LogAsync(Exception exception, string message = "", bool writeToConsole = true)
{
if (IsDisabled)
{
return;
}
if (ELogType.ERROR <= MaxLogType)
{
await LogManager.WriteAsync(exception, message, writeToConsole: writeToConsole).ConfigureAwait(false);
await _logManager.WriteAsync(exception, message, writeToConsole: writeToConsole).ConfigureAwait(false);
}
}
public static void Configure()
{
var loggerSettings = new LoggerSettings
{
Id = "EonaCatTestLogger",
MaxLogType = ELogType.INFO,
UseLocalTime = UseLocalTime,
FileLoggerOptions =
{
LogDirectory = LogFolder,
FileSizeLimit = 20_000_000, // 20 MB,
UseLocalTime = UseLocalTime
}
};
LogManager = new LogManager(loggerSettings);
}
}