using System.Text; namespace Gatedge.ScanCode.Common { /// /// 文件日志帮助类 /// public class FileLogger : ILogger { private readonly string _categoryName; private readonly string _filePath; private readonly long _maxFileSize; // 最大文件大小(字节) private readonly int _maxRetainedFiles; // 最大保留文件数 /// /// 构造函数 /// /// /// /// /// public FileLogger(string categoryName, string filePath, long maxFileSize = 10 * 1024 * 1024, int maxRetainedFiles = 5) { _categoryName = categoryName; _filePath = filePath; _maxFileSize = maxFileSize; _maxRetainedFiles = maxRetainedFiles; } /// /// 开始 /// /// /// /// public IDisposable BeginScope(TState state) => null; /// /// 检测日志等级是否启用 /// /// /// public bool IsEnabled(LogLevel logLevel) => true; /// /// 写入日志 /// /// /// /// /// /// /// public void Log( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { // 过滤日志级别(可选) if (!IsEnabled(logLevel)) return; // 格式化日志消息 var message = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{logLevel}] {_categoryName}: {formatter(state, exception)}{Environment.NewLine}"; // 确保目录存在 var directory = Path.GetDirectoryName(_filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // 检查文件大小并滚动 RollFileIfNeeded(); // 写入日志 File.AppendAllText(_filePath, message, Encoding.UTF8); } /// /// 文件滚动刷新 /// private void RollFileIfNeeded() { try { var fileInfo = new FileInfo(_filePath); if (fileInfo.Exists && fileInfo.Length >= _maxFileSize) { // 滚动文件命名:logs/app_20231005_001.log var timestamp = DateTime.Now.ToString("yyyyMMdd"); var basePath = Path.Combine( Path.GetDirectoryName(_filePath), Path.GetFileNameWithoutExtension(_filePath) ); var newFilePath = $"{basePath}_{timestamp}_{Guid.NewGuid():n}{Path.GetExtension(_filePath)}"; File.Move(_filePath, newFilePath); // 清理旧文件 var directory = new DirectoryInfo(Path.GetDirectoryName(_filePath)); var files = directory.GetFiles($"{Path.GetFileNameWithoutExtension(_filePath)}_*") .OrderByDescending(f => f.CreationTime) .Skip(_maxRetainedFiles - 1) .ToList(); foreach (var file in files) { file.Delete(); } } } catch { // 处理文件操作异常(如权限不足) } } } /// /// 日志写入帮助类 /// public class FileLoggerProvider : ILoggerProvider { private readonly string _filePath; private readonly long _maxFileSize; private readonly int _maxRetainedFiles; /// /// 构造函数 /// /// /// /// public FileLoggerProvider(string filePath, long maxFileSize, int maxRetainedFiles) { _filePath = filePath; _maxFileSize = maxFileSize; _maxRetainedFiles = maxRetainedFiles; } /// /// 创建日志写入器 /// /// /// public ILogger CreateLogger(string categoryName) { return new FileLogger(categoryName, _filePath, _maxFileSize, _maxRetainedFiles); } /// /// 清理资源 /// public void Dispose() { // 清理资源(如需要) } } }