You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.7 KiB
C#
171 lines
5.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Haoliang.Models.System;
|
|
using Haoliang.Data.Repositories;
|
|
|
|
namespace Haoliang.Data.Repositories
|
|
{
|
|
public interface ILogRepository : IRepository<LogEntry>
|
|
{
|
|
Task<IEnumerable<LogEntry>> GetLogsAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null, string category = null);
|
|
Task<int> GetLogCountAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null);
|
|
Task ArchiveLogsAsync(DateTime cutoffDate);
|
|
Task ClearLogsAsync();
|
|
Task<IEnumerable<LogEntry>> GetRecentLogsAsync(int count = 100);
|
|
Task<IEnumerable<LogEntry>> GetErrorLogsAsync(DateTime? startDate = null, DateTime? endDate = null);
|
|
Task<LogStatistics> GetLogStatisticsAsync(DateTime date);
|
|
Task<bool> LogExistsAsync(string logId);
|
|
Task<IEnumerable<LogEntry>> GetLogsBySourceAsync(string source);
|
|
}
|
|
|
|
public class LogRepository : Repository<LogEntry>, ILogRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public LogRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<LogEntry>> GetLogsAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null, string category = null)
|
|
{
|
|
var query = _context.Logs.AsQueryable();
|
|
|
|
if (logLevel.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogLevel == logLevel.Value);
|
|
}
|
|
|
|
if (startDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime >= startDate.Value);
|
|
}
|
|
|
|
if (endDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime <= endDate.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(category))
|
|
{
|
|
query = query.Where(l => l.Category == category);
|
|
}
|
|
|
|
return await query
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> GetLogCountAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null)
|
|
{
|
|
var query = _context.Logs.AsQueryable();
|
|
|
|
if (logLevel.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogLevel == logLevel.Value);
|
|
}
|
|
|
|
if (startDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime >= startDate.Value);
|
|
}
|
|
|
|
if (endDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime <= endDate.Value);
|
|
}
|
|
|
|
return await query.CountAsync();
|
|
}
|
|
|
|
public async Task ArchiveLogsAsync(DateTime cutoffDate)
|
|
{
|
|
var logsToArchive = await _context.Logs
|
|
.Where(l => l.LogTime < cutoffDate)
|
|
.ToListAsync();
|
|
|
|
if (logsToArchive.Any())
|
|
{
|
|
// In a real implementation, you would move these to an archive table or file
|
|
// For now, we'll just delete them
|
|
_context.Logs.RemoveRange(logsToArchive);
|
|
await SaveAsync();
|
|
}
|
|
}
|
|
|
|
public async Task ClearLogsAsync()
|
|
{
|
|
_context.Logs.RemoveRange(_context.Logs);
|
|
await SaveAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<LogEntry>> GetRecentLogsAsync(int count = 100)
|
|
{
|
|
return await _context.Logs
|
|
.OrderByDescending(l => l.LogTime)
|
|
.Take(count)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<LogEntry>> GetErrorLogsAsync(DateTime? startDate = null, DateTime? endDate = null)
|
|
{
|
|
var query = _context.Logs
|
|
.Where(l => l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical);
|
|
|
|
if (startDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime >= startDate.Value);
|
|
}
|
|
|
|
if (endDate.HasValue)
|
|
{
|
|
query = query.Where(l => l.LogTime <= endDate.Value);
|
|
}
|
|
|
|
return await query
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<LogStatistics> GetLogStatisticsAsync(DateTime date)
|
|
{
|
|
var startOfDay = date.Date;
|
|
var endOfDay = startOfDay.AddDays(1);
|
|
|
|
var logs = await _context.Logs
|
|
.Where(l => l.LogTime >= startOfDay && l.LogTime <= endOfDay)
|
|
.ToListAsync();
|
|
|
|
var stats = new LogStatistics
|
|
{
|
|
Date = date,
|
|
TotalLogs = logs.Count,
|
|
ErrorLogs = logs.Count(l => l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical),
|
|
WarningLogs = logs.Count(l => l.LogLevel == LogLevel.Warning),
|
|
InfoLogs = logs.Count(l => l.LogLevel == LogLevel.Information),
|
|
DebugLogs = logs.Count(l => l.LogLevel == LogLevel.Debug),
|
|
LogSources = logs.GroupBy(l => l.Source)
|
|
.ToDictionary(g => g.Key, g => g.Count())
|
|
};
|
|
|
|
return stats;
|
|
}
|
|
|
|
public async Task<bool> LogExistsAsync(string logId)
|
|
{
|
|
return await _context.Logs
|
|
.AnyAsync(l => l.LogId == logId);
|
|
}
|
|
|
|
public async Task<IEnumerable<LogEntry>> GetLogsBySourceAsync(string source)
|
|
{
|
|
return await _context.Logs
|
|
.Where(l => l.Source == source)
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
} |