Updated
This commit is contained in:
@@ -1,118 +1,120 @@
|
||||
using EonaCat.Logger.Managers;
|
||||
using System.IO.Compression;
|
||||
using System.IO.Compression;
|
||||
using EonaCat.Logger.EonaCatCoreLogger;
|
||||
using EonaCat.Logger.Extensions;
|
||||
using EonaCat.Logger.Managers;
|
||||
|
||||
namespace EonaCat.Logger.Test.Web
|
||||
namespace EonaCat.Logger.Test.Web;
|
||||
|
||||
public static class Logger
|
||||
{
|
||||
public static class Logger
|
||||
private static LogManager LogManager;
|
||||
public static ELogType MinLogType { 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; }
|
||||
|
||||
public static void DeleteCurrentLogFile()
|
||||
{
|
||||
public static ELogType MaxLogType { get; set; }
|
||||
public static bool UseLocalTime { get; set; }
|
||||
private static LogManager LogManager;
|
||||
public static string LogFolder => "Logs";
|
||||
public static string CurrentLogFile => LogManager.CurrentLogFile;
|
||||
public static bool IsDisabled { get; set; }
|
||||
if (IsDisabled)
|
||||
return;
|
||||
|
||||
public static void DeleteCurrentLogFile()
|
||||
LogManager.DeleteCurrentLogFile();
|
||||
}
|
||||
|
||||
private static string ConvertToAbsolutePath(string path)
|
||||
{
|
||||
return Path.IsPathRooted(path) ? path : Path.Combine(LogFolder, path);
|
||||
}
|
||||
|
||||
public static async Task DownloadLogAsync(HttpContext context, string logName, long limit)
|
||||
{
|
||||
var logFileName = logName + ".log";
|
||||
|
||||
await using var fS = new FileStream(Path.Combine(ConvertToAbsolutePath(LogFolder), logFileName), FileMode.Open,
|
||||
FileAccess.Read, FileShare.ReadWrite, 64 * 1024, true);
|
||||
var response = context.Response;
|
||||
|
||||
response.ContentType = "text/plain";
|
||||
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;
|
||||
Stream stream;
|
||||
|
||||
string acceptEncoding = request.Headers["Accept-Encoding"];
|
||||
if (string.IsNullOrEmpty(acceptEncoding))
|
||||
{
|
||||
if (IsDisabled)
|
||||
return;
|
||||
|
||||
LogManager.DeleteCurrentLogFile();
|
||||
stream = response.Body;
|
||||
}
|
||||
|
||||
private static string ConvertToAbsolutePath(string path)
|
||||
else
|
||||
{
|
||||
return Path.IsPathRooted(path) ? path : Path.Combine(LogFolder, path);
|
||||
}
|
||||
var acceptEncodingParts = acceptEncoding.Split(new[] { ',' },
|
||||
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
public static async Task DownloadLogAsync(HttpContext context, string logName, long limit)
|
||||
{
|
||||
var logFileName = logName + ".log";
|
||||
|
||||
await using var fS = new FileStream(Path.Combine(ConvertToAbsolutePath(LogFolder), logFileName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 64 * 1024, true);
|
||||
var response = context.Response;
|
||||
|
||||
response.ContentType = "text/plain";
|
||||
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;
|
||||
Stream stream;
|
||||
|
||||
string acceptEncoding = request.Headers["Accept-Encoding"];
|
||||
if (string.IsNullOrEmpty(acceptEncoding))
|
||||
if (acceptEncodingParts.Contains("br"))
|
||||
{
|
||||
stream = response.Body;
|
||||
response.Headers.ContentEncoding = "br";
|
||||
stream = new BrotliStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else if (acceptEncodingParts.Contains("gzip"))
|
||||
{
|
||||
response.Headers.ContentEncoding = "gzip";
|
||||
stream = new GZipStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else if (acceptEncodingParts.Contains("deflate"))
|
||||
{
|
||||
response.Headers.ContentEncoding = "deflate";
|
||||
stream = new DeflateStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] acceptEncodingParts = acceptEncoding.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (acceptEncodingParts.Contains("br"))
|
||||
{
|
||||
response.Headers.ContentEncoding = "br";
|
||||
stream = new BrotliStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else if (acceptEncodingParts.Contains("gzip"))
|
||||
{
|
||||
response.Headers.ContentEncoding = "gzip";
|
||||
stream = new GZipStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else if (acceptEncodingParts.Contains("deflate"))
|
||||
{
|
||||
response.Headers.ContentEncoding = "deflate";
|
||||
stream = new DeflateStream(response.Body, CompressionMode.Compress);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream = response.Body;
|
||||
}
|
||||
}
|
||||
|
||||
await using (stream)
|
||||
{
|
||||
await oFS.CopyToAsync(stream).ConfigureAwait(false);
|
||||
|
||||
if (fS.Length > limit)
|
||||
await stream.WriteAsync("\r\n####__EONACATLOGGER_TRUNCATED___####"u8.ToArray()).ConfigureAwait(false);
|
||||
stream = response.Body;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Log(string message, ELogType logType = ELogType.INFO, bool writeToConsole = true)
|
||||
await using (stream)
|
||||
{
|
||||
if (IsDisabled)
|
||||
return;
|
||||
await oFS.CopyToAsync(stream).ConfigureAwait(false);
|
||||
|
||||
LogManager.Write(message, logType, writeToConsole);
|
||||
}
|
||||
|
||||
public static void Log(Exception exception, string message = "", bool writeToConsole = true)
|
||||
{
|
||||
if (IsDisabled)
|
||||
return;
|
||||
|
||||
LogManager.Write(exception, module: message, writeToConsole: writeToConsole);
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
var loggerSettings = new LoggerSettings
|
||||
{
|
||||
Id = "EonaCatDnsLogger",
|
||||
MaxLogType = ELogType.TRACE,
|
||||
UseLocalTime = UseLocalTime,
|
||||
FileLoggerOptions =
|
||||
{
|
||||
LogDirectory = "Logs",
|
||||
FileSizeLimit = 20_000_000, // 20 MB,
|
||||
UseLocalTime = UseLocalTime,
|
||||
}
|
||||
};
|
||||
LogManager = new LogManager(loggerSettings);
|
||||
if (fS.Length > limit)
|
||||
await stream.WriteAsync("\r\n####__EONACATLOGGER_TRUNCATED___####"u8.ToArray()).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Log(string message, ELogType logType = ELogType.INFO, bool writeToConsole = true)
|
||||
{
|
||||
if (IsDisabled)
|
||||
return;
|
||||
|
||||
LogManager.Write(message, logType, writeToConsole);
|
||||
}
|
||||
|
||||
public static void Log(Exception exception, string message = "", bool writeToConsole = true)
|
||||
{
|
||||
if (IsDisabled)
|
||||
return;
|
||||
|
||||
LogManager.Write(exception, message, writeToConsole: writeToConsole);
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
var loggerSettings = new LoggerSettings
|
||||
{
|
||||
Id = "EonaCatTestLogger",
|
||||
MinLogType = ELogType.INFO,
|
||||
UseLocalTime = UseLocalTime,
|
||||
FileLoggerOptions =
|
||||
{
|
||||
LogDirectory = LogFolder,
|
||||
FileSizeLimit = 20_000_000, // 20 MB,
|
||||
UseLocalTime = UseLocalTime
|
||||
}
|
||||
};
|
||||
LogManager = new LogManager(loggerSettings);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user