544 lines
16 KiB
C#
544 lines
16 KiB
C#
using EonaCat.Logger;
|
|
using EonaCat.Logger.EonaCatCoreLogger;
|
|
using EonaCat.Logger.EonaCatCoreLogger.Internal;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Channels;
|
|
using System.Threading.Tasks;
|
|
|
|
public enum LogOverflowStrategy
|
|
{
|
|
Wait,
|
|
DropNewest,
|
|
DropOldest,
|
|
}
|
|
|
|
[ProviderAlias("EonaCatFileLogger")]
|
|
public sealed class FileLoggerProvider : BatchingLoggerProvider, IDisposable
|
|
{
|
|
private const int BufferSize = 64 * 1024;
|
|
private const int ChannelCapacity = 8192;
|
|
private const int FlushThreshold = 48 * 1024;
|
|
private static readonly UTF8Encoding Utf8 = new(false);
|
|
|
|
private readonly Channel<LogMessage> _channel;
|
|
private readonly Task _writerTask;
|
|
private int _flushRequested;
|
|
|
|
private string _filePath;
|
|
private readonly int _maxFileSize;
|
|
private readonly int _maxRolloverFiles;
|
|
private readonly bool _encryptionEnabled;
|
|
|
|
private FileStream _fileStream;
|
|
private CryptoStream? _cryptoStream;
|
|
private byte[] _buffer;
|
|
private int _position;
|
|
private long _size;
|
|
|
|
private readonly Aes? _aes;
|
|
public event Action<Exception>? OnError;
|
|
public event Action<string>? OnFileRolled;
|
|
|
|
public bool IncludeCorrelationId { get; }
|
|
public bool EnableCategoryRouting { get; }
|
|
public CompressionLevel CompressionLevel { get; set; } = CompressionLevel.Optimal;
|
|
|
|
public string LogFile => _filePath;
|
|
|
|
[ThreadStatic]
|
|
private static StringBuilder? _cachedStringBuilder;
|
|
|
|
private volatile bool _running = true;
|
|
public ELogType MinimumLogLevel { get; set; }
|
|
private readonly LoggerScopedContext _context = new();
|
|
|
|
private static readonly TimeSpan FlushInterval = TimeSpan.FromMilliseconds(500);
|
|
private long _lastFlushTicks = DateTime.UtcNow.Ticks;
|
|
private DateTime _currentRollDate = DateTime.UtcNow.Date;
|
|
private readonly Channel<string> _compressQueue = Channel.CreateUnbounded<string>();
|
|
|
|
|
|
public FileLoggerProvider(IOptions<FileLoggerOptions> options) : base(options)
|
|
{
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
|
|
AppDomain.CurrentDomain.UnhandledException += (s, e) => Dispose();
|
|
|
|
var o = options.Value;
|
|
string primaryDirectory = o.LogDirectory;
|
|
string fileName = $"{o.FileNamePrefix}_{Environment.MachineName}_{DateTime.UtcNow:yyyyMMdd}.log";
|
|
_filePath = Path.Combine(primaryDirectory, fileName);
|
|
|
|
if (!TryInitializePath(primaryDirectory, fileName))
|
|
{
|
|
string tempDirectory = Path.GetTempPath();
|
|
string fallbackFileName = $"EonaCat_{DateTime.UtcNow:yyyyMMdd}.log";
|
|
if (!TryInitializePath(tempDirectory, fallbackFileName))
|
|
{
|
|
_running = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
_maxFileSize = o.FileSizeLimit;
|
|
_maxRolloverFiles = o.MaxRolloverFiles;
|
|
_encryptionEnabled = o.EncryptionKey != null && o.EncryptionIV != null;
|
|
|
|
if (_encryptionEnabled)
|
|
{
|
|
_aes = Aes.Create();
|
|
_aes.Key = o.EncryptionKey;
|
|
_aes.IV = o.EncryptionIV;
|
|
}
|
|
|
|
IncludeCorrelationId = o.IncludeCorrelationId;
|
|
EnableCategoryRouting = o.EnableCategoryRouting;
|
|
|
|
_buffer = ArrayPool<byte>.Shared.Rent(BufferSize);
|
|
|
|
_channel = Channel.CreateBounded<LogMessage>(new BoundedChannelOptions(ChannelCapacity)
|
|
{
|
|
SingleReader = true,
|
|
SingleWriter = false,
|
|
FullMode = o.OverflowStrategy switch
|
|
{
|
|
LogOverflowStrategy.DropNewest => BoundedChannelFullMode.DropWrite,
|
|
LogOverflowStrategy.DropOldest => BoundedChannelFullMode.DropOldest,
|
|
_ => BoundedChannelFullMode.Wait
|
|
}
|
|
});
|
|
|
|
StartCompressionWorker();
|
|
_writerTask = Task.Run(WriterLoopAsync);
|
|
}
|
|
|
|
private void StartCompressionWorker()
|
|
{
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await foreach (var path in _compressQueue.Reader.ReadAllAsync())
|
|
{
|
|
try
|
|
{
|
|
string dest = GetRotatedGzipPath(path);
|
|
|
|
using var input = File.OpenRead(path);
|
|
using var output = File.Create(dest);
|
|
using var gzip = new GZipStream(output, CompressionLevel);
|
|
await input.CopyToAsync(gzip);
|
|
|
|
File.Delete(path);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RaiseError(ex);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// Generates a new rotated .gz filename and shifts older files
|
|
private string GetRotatedGzipPath(string originalPath)
|
|
{
|
|
const int MaxFiles = 5; // maximum number of gz files to keep
|
|
string dir = Path.GetDirectoryName(originalPath) ?? "";
|
|
string name = Path.GetFileNameWithoutExtension(originalPath);
|
|
|
|
// Shift existing files: log_4.gz → log_5.gz, log_3.gz → log_4.gz, ...
|
|
for (int i = MaxFiles - 1; i >= 1; i--)
|
|
{
|
|
string oldFile = Path.Combine(dir, $"{name}_{i}.gz");
|
|
if (File.Exists(oldFile))
|
|
{
|
|
string newFile = Path.Combine(dir, $"{name}_{i + 1}.gz");
|
|
|
|
// Delete the destination if it already exists
|
|
if (File.Exists(newFile))
|
|
File.Delete(newFile);
|
|
|
|
File.Move(oldFile, newFile);
|
|
}
|
|
}
|
|
|
|
// New file becomes _1.gz
|
|
return Path.Combine(dir, $"{name}_1.gz");
|
|
}
|
|
|
|
private bool TryInitializePath(string directory, string fileName)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
string fullPath = Path.Combine(directory, fileName);
|
|
if (!EnsureWritable(fullPath))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_filePath = fullPath;
|
|
_fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write,
|
|
FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.SequentialScan | FileOptions.WriteThrough);
|
|
|
|
if (_encryptionEnabled && _aes != null)
|
|
{
|
|
_cryptoStream = new CryptoStream(_fileStream, _aes.CreateEncryptor(), CryptoStreamMode.Write);
|
|
}
|
|
|
|
_size = _fileStream.Length;
|
|
return true;
|
|
}
|
|
catch (Exception ex) { RaiseError(ex); return false; }
|
|
}
|
|
|
|
internal override Task WriteMessagesAsync(IReadOnlyList<LogMessage> messages, CancellationToken token)
|
|
{
|
|
foreach (var msg in messages)
|
|
{
|
|
if (msg.Level >= MinimumLogLevel)
|
|
{
|
|
while (!_channel.Writer.TryWrite(msg))
|
|
{
|
|
Thread.SpinWait(1);
|
|
}
|
|
}
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private bool NeedsTimeRoll()
|
|
{
|
|
return DateTime.UtcNow.Date > _currentRollDate;
|
|
}
|
|
|
|
private async Task WriterLoopAsync()
|
|
{
|
|
var reader = _channel.Reader;
|
|
var batch = new List<LogMessage>(256);
|
|
|
|
while (await reader.WaitToReadAsync())
|
|
{
|
|
batch.Clear();
|
|
while (reader.TryRead(out var msg))
|
|
batch.Add(msg);
|
|
|
|
if (batch.Count > 0)
|
|
{
|
|
if (NeedsTimeRoll())
|
|
{
|
|
_currentRollDate = DateTime.UtcNow.Date;
|
|
await RollFileAsync();
|
|
}
|
|
await WriteBatchAsync(batch);
|
|
}
|
|
}
|
|
|
|
await FlushFinalAsync();
|
|
}
|
|
|
|
private async Task WriteBatchAsync(IReadOnlyList<LogMessage> batch)
|
|
{
|
|
var sb = AcquireStringBuilder();
|
|
foreach (var msg in batch)
|
|
{
|
|
if (IncludeCorrelationId)
|
|
{
|
|
var ctx = _context.GetAll();
|
|
if (ctx.Count > 0)
|
|
{
|
|
sb.Append(" [");
|
|
foreach (var kv in ctx)
|
|
{
|
|
sb.Append(kv.Key).Append('=').Append(kv.Value).Append(' ');
|
|
}
|
|
|
|
sb.Length--;
|
|
sb.Append(']');
|
|
}
|
|
}
|
|
sb.Append(' ').Append(msg.Message).AppendLine();
|
|
}
|
|
|
|
// Directly encode to pooled byte buffer
|
|
int maxBytes = Utf8.GetMaxByteCount(sb.Length);
|
|
if (maxBytes > _buffer.Length - _position)
|
|
{
|
|
await FlushInternalAsync();
|
|
}
|
|
|
|
int bytesWritten = Utf8.GetBytes(sb.ToString(), 0, sb.Length, _buffer, _position);
|
|
_position += bytesWritten;
|
|
_size += bytesWritten;
|
|
|
|
ReleaseStringBuilder(sb);
|
|
|
|
if (_maxFileSize > 0 && _size >= _maxFileSize)
|
|
{
|
|
await RollFileAsync();
|
|
}
|
|
|
|
await FlushIfNeededAsync();
|
|
}
|
|
|
|
private async Task FlushIfNeededAsync()
|
|
{
|
|
long now = DateTime.UtcNow.Ticks;
|
|
if (_position >= FlushThreshold || now - _lastFlushTicks >= FlushInterval.Ticks)
|
|
{
|
|
if (Interlocked.Exchange(ref _flushRequested, 1) == 0)
|
|
{
|
|
try
|
|
{
|
|
await FlushInternalAsync();
|
|
_lastFlushTicks = now;
|
|
}
|
|
finally { _flushRequested = 0; }
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task FlushInternalAsync()
|
|
{
|
|
if (_position == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (NeedsReopen())
|
|
{
|
|
await ReopenFileAsync();
|
|
}
|
|
|
|
if (_cryptoStream != null)
|
|
{
|
|
await _cryptoStream.WriteAsync(_buffer, 0, _position);
|
|
await _cryptoStream.FlushAsync();
|
|
}
|
|
else
|
|
{
|
|
await _fileStream.WriteAsync(_buffer, 0, _position);
|
|
await _fileStream.FlushAsync();
|
|
}
|
|
}
|
|
catch (Exception ex) { RaiseError(ex); }
|
|
|
|
_position = 0;
|
|
}
|
|
|
|
|
|
private bool NeedsReopen()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(_filePath))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var info = new FileInfo(_filePath);
|
|
return info.Length < _size;
|
|
}
|
|
catch { return false; }
|
|
}
|
|
|
|
private async Task ReopenFileAsync()
|
|
{
|
|
_cryptoStream?.Dispose();
|
|
_fileStream?.Dispose();
|
|
|
|
_fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write,
|
|
FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.SequentialScan | FileOptions.WriteThrough);
|
|
|
|
if (_encryptionEnabled && _aes != null)
|
|
{
|
|
_cryptoStream = new CryptoStream(_fileStream, _aes.CreateEncryptor(), CryptoStreamMode.Write);
|
|
}
|
|
|
|
_size = _fileStream.Length;
|
|
}
|
|
|
|
private async Task FlushFinalAsync()
|
|
{
|
|
await FlushInternalAsync();
|
|
_cryptoStream?.FlushFinalBlock();
|
|
await _fileStream.FlushAsync();
|
|
}
|
|
|
|
private async Task RollFileAsync()
|
|
{
|
|
try
|
|
{
|
|
await FlushInternalAsync();
|
|
|
|
_cryptoStream?.FlushFinalBlock();
|
|
_cryptoStream?.Dispose();
|
|
_cryptoStream = null;
|
|
_fileStream?.Dispose();
|
|
|
|
string directory = Path.GetDirectoryName(_filePath)!;
|
|
string baseName = Path.GetFileNameWithoutExtension(_filePath);
|
|
string extension = Path.GetExtension(_filePath);
|
|
|
|
// Shift existing rollover files
|
|
for (int i = _maxRolloverFiles - 1; i >= 1; i--)
|
|
{
|
|
string src = Path.Combine(directory, $"{baseName}_{i}{extension}");
|
|
string dest = Path.Combine(directory, $"{baseName}_{i + 1}{extension}");
|
|
if (File.Exists(dest))
|
|
{
|
|
// Compress oldest if it exceeds max
|
|
_compressQueue.Writer.TryWrite(dest);
|
|
File.Delete(dest);
|
|
}
|
|
|
|
if (File.Exists(src))
|
|
{
|
|
File.Move(src, dest);
|
|
}
|
|
}
|
|
|
|
// Move current log to FORMAT_1.log
|
|
string firstRollover = Path.Combine(directory, $"{baseName}_1{extension}");
|
|
if (File.Exists(_filePath))
|
|
{
|
|
File.Move(_filePath, firstRollover);
|
|
}
|
|
|
|
// Compress if we exceed max rollover
|
|
string oldest = Path.Combine(directory, $"{baseName}_{_maxRolloverFiles + 1}{extension}");
|
|
if (File.Exists(oldest))
|
|
{
|
|
_compressQueue.Writer.TryWrite(oldest);
|
|
File.Delete(oldest);
|
|
}
|
|
|
|
// Recreate active log
|
|
RecreateLogFile();
|
|
|
|
OnFileRolled?.Invoke(firstRollover);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RaiseError(ex);
|
|
}
|
|
}
|
|
|
|
|
|
private void RecreateLogFile()
|
|
{
|
|
_fileStream = new FileStream(_filePath,
|
|
FileMode.Create,
|
|
FileAccess.Write,
|
|
FileShare.ReadWrite | FileShare.Delete,
|
|
4096,
|
|
FileOptions.SequentialScan | FileOptions.WriteThrough);
|
|
|
|
if (_encryptionEnabled && _aes != null)
|
|
{
|
|
_cryptoStream = new CryptoStream(_fileStream, _aes.CreateEncryptor(), CryptoStreamMode.Write);
|
|
}
|
|
|
|
_size = 0;
|
|
}
|
|
|
|
private void CleanupOldRollovers(string directory, string baseName)
|
|
{
|
|
if (_maxRolloverFiles <= 0)
|
|
return;
|
|
|
|
var rolledLogs = Directory.GetFiles(directory, $"{baseName}_*.log")
|
|
.Where(file => !file.EndsWith(".gz", StringComparison.OrdinalIgnoreCase))
|
|
.Select(file => new FileInfo(file))
|
|
.OrderByDescending(file => file.CreationTimeUtc)
|
|
.ToList();
|
|
|
|
// If too many .log rollovers → compress oldest
|
|
if (rolledLogs.Count > _maxRolloverFiles)
|
|
{
|
|
foreach (var file in rolledLogs.Skip(_maxRolloverFiles))
|
|
{
|
|
try
|
|
{
|
|
_compressQueue.Writer.TryWrite(file.FullName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RaiseError(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static StringBuilder AcquireStringBuilder()
|
|
{
|
|
var sb = _cachedStringBuilder;
|
|
if (sb == null)
|
|
{
|
|
sb = new StringBuilder(256);
|
|
}
|
|
else
|
|
{
|
|
sb.Clear();
|
|
}
|
|
|
|
_cachedStringBuilder = sb;
|
|
return sb;
|
|
}
|
|
|
|
private static void ReleaseStringBuilder(StringBuilder sb)
|
|
{
|
|
if (sb.Capacity > 8 * 1024)
|
|
{
|
|
_cachedStringBuilder = new StringBuilder(256);
|
|
}
|
|
}
|
|
|
|
private bool EnsureWritable(string path)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete);
|
|
return true;
|
|
}
|
|
catch (Exception ex) { RaiseError(ex); return false; }
|
|
}
|
|
|
|
private void RaiseError(Exception ex)
|
|
{
|
|
try { OnError?.Invoke(ex); } catch { }
|
|
}
|
|
|
|
protected override async Task OnShutdownFlushAsync()
|
|
{
|
|
_running = false;
|
|
|
|
try
|
|
{
|
|
_channel.Writer.Complete();
|
|
}
|
|
catch
|
|
{
|
|
// Channel closed before we could complete, ignore
|
|
}
|
|
await _writerTask;
|
|
|
|
ArrayPool<byte>.Shared.Return(_buffer, true);
|
|
_cryptoStream?.Dispose();
|
|
_fileStream.Dispose();
|
|
_aes?.Dispose();
|
|
}
|
|
|
|
public new void Dispose() => OnShutdownFlushAsync().GetAwaiter().GetResult();
|
|
}
|