Torrent-Manager/src/Services/LoggingService.cs
MasterDraco f21639455d fix: Implement required interface methods and address nullable warnings
- Added GetConfiguration() and SaveConfigurationAsync() methods to ConfigService
- Added synchronous file reading for GetConfiguration()
- Fixed nullable reference type warnings in LoggingService
- Added nullable annotations to interface definitions
- Enabled nullable reference types for affected files

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-12 22:36:39 +00:00

219 lines
7.5 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace TransmissionRssManager.Services
{
public class LogEntry
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public string Level { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public string Context { get; set; } = string.Empty;
public string Properties { get; set; } = string.Empty;
}
public class UserPreferences
{
public bool EnableDarkMode { get; set; } = false;
public bool AutoRefreshUIEnabled { get; set; } = true;
public int AutoRefreshIntervalSeconds { get; set; } = 30;
public bool NotificationsEnabled { get; set; } = true;
public List<string> NotificationEvents { get; set; } = new List<string>
{
"torrent-added",
"torrent-completed",
"torrent-error"
};
public string DefaultView { get; set; } = "dashboard";
public bool ConfirmBeforeDelete { get; set; } = true;
public int MaxItemsPerPage { get; set; } = 25;
public string DateTimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";
public bool ShowCompletedTorrents { get; set; } = true;
public int KeepHistoryDays { get; set; } = 30;
}
public interface ILoggingService
{
void Configure(UserPreferences preferences);
Task<List<LogEntry>> GetLogsAsync(LogFilterOptions options);
Task ClearLogsAsync(DateTime? olderThan = null);
Task<byte[]> ExportLogsAsync(LogFilterOptions options);
void Log(LogLevel level, string message, string? context = null, Dictionary<string, string>? properties = null);
}
public class LogFilterOptions
{
public string Level { get; set; } = "All";
public string Search { get; set; } = "";
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string Context { get; set; } = "";
public int Limit { get; set; } = 100;
public int Offset { get; set; } = 0;
}
public class LoggingService : ILoggingService
{
private readonly ILogger<LoggingService> _logger;
private readonly string _logFilePath;
private readonly object _logLock = new object();
private List<LogEntry> _inMemoryLogs = new List<LogEntry>();
private readonly int _maxLogEntries = 1000;
public LoggingService(ILogger<LoggingService> logger)
{
_logger = logger;
// Prepare log directory and file
var logsDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
Directory.CreateDirectory(logsDirectory);
_logFilePath = Path.Combine(logsDirectory, "application_logs.json");
// Initialize log file if it doesn't exist
if (!File.Exists(_logFilePath))
{
File.WriteAllText(_logFilePath, "[]");
}
// Load existing logs into memory
try
{
var json = File.ReadAllText(_logFilePath);
_inMemoryLogs = JsonSerializer.Deserialize<List<LogEntry>>(json) ?? new List<LogEntry>();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to load logs from file");
_inMemoryLogs = new List<LogEntry>();
}
}
public void Configure(UserPreferences preferences)
{
// No-op in simplified version
}
public Task<List<LogEntry>> GetLogsAsync(LogFilterOptions options)
{
var filteredLogs = _inMemoryLogs.AsEnumerable();
// Apply filters
if (!string.IsNullOrEmpty(options.Level) && options.Level != "All")
{
filteredLogs = filteredLogs.Where(l => l.Level == options.Level);
}
if (!string.IsNullOrEmpty(options.Search))
{
filteredLogs = filteredLogs.Where(l =>
l.Message.Contains(options.Search, StringComparison.OrdinalIgnoreCase));
}
if (options.StartDate.HasValue)
{
filteredLogs = filteredLogs.Where(l => l.Timestamp >= options.StartDate.Value);
}
if (options.EndDate.HasValue)
{
filteredLogs = filteredLogs.Where(l => l.Timestamp <= options.EndDate.Value);
}
if (!string.IsNullOrEmpty(options.Context))
{
filteredLogs = filteredLogs.Where(l => l.Context == options.Context);
}
// Sort, paginate and return
return Task.FromResult(
filteredLogs
.OrderByDescending(l => l.Timestamp)
.Skip(options.Offset)
.Take(options.Limit)
.ToList()
);
}
public Task ClearLogsAsync(DateTime? olderThan = null)
{
lock (_logLock)
{
if (olderThan.HasValue)
{
_inMemoryLogs.RemoveAll(l => l.Timestamp < olderThan.Value);
}
else
{
_inMemoryLogs.Clear();
}
SaveLogs();
}
return Task.CompletedTask;
}
public async Task<byte[]> ExportLogsAsync(LogFilterOptions options)
{
var logs = await GetLogsAsync(options);
var json = JsonSerializer.Serialize(logs, new JsonSerializerOptions { WriteIndented = true });
return Encoding.UTF8.GetBytes(json);
}
public void Log(LogLevel level, string message, string? context = null, Dictionary<string, string>? properties = null)
{
var levelString = level.ToString();
// Log to standard logger
_logger.Log(level, message);
// Store in our custom log system
var entry = new LogEntry
{
Id = _inMemoryLogs.Count > 0 ? _inMemoryLogs.Max(l => l.Id) + 1 : 1,
Timestamp = DateTime.UtcNow,
Level = levelString,
Message = message,
Context = context ?? "System",
Properties = properties != null ? JsonSerializer.Serialize(properties) : "{}"
};
lock (_logLock)
{
_inMemoryLogs.Add(entry);
// Keep log size under control
if (_inMemoryLogs.Count > _maxLogEntries)
{
_inMemoryLogs = _inMemoryLogs
.OrderByDescending(l => l.Timestamp)
.Take(_maxLogEntries)
.ToList();
}
SaveLogs();
}
}
private void SaveLogs()
{
try
{
var json = JsonSerializer.Serialize(_inMemoryLogs);
File.WriteAllText(_logFilePath, json);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save logs to file");
}
}
}
}