You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone.Core/Instrumentation/LogProvider.cs

59 lines
1.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using PetaPoco;
namespace NzbDrone.Core.Instrumentation
{
public class LogProvider
{
private readonly IDatabase _database;
private readonly LogDbContext _logDbContext;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public LogProvider(IDatabase database, LogDbContext logDbContext)
{
_database = database;
_logDbContext = logDbContext;
}
public IQueryable<Log> GetAllLogs()
{
return _logDbContext.Logs;
}
public IList<Log> TopLogs(int count)
{
var logs = _database.Fetch<Log>("SELECT TOP " + count + " * FROM Logs ORDER BY Time Desc");
logs.Add(new Log
{
Time = DateTime.Now.AddYears(-100),
Level = "Info",
Logger = "Core.Instrumentation.LogProvider",
Message = String.Format("Number of logs currently shown: {0}. More may exist, check 'All' to see everything", Math.Min(count, logs.Count))
});
return logs;
}
public Page<Log> GetPagedLogs(int pageNumber, int pageSize)
{
return _database.Page<Log>(pageNumber, pageSize, "SELECT * FROM Logs ORDER BY Time DESC");
}
public void DeleteAll()
{
_database.Delete<Log>("");
Logger.Info("Cleared Log History");
}
public void Trim()
{
_database.Delete<Log>("WHERE Time < @0", DateTime.Now.AddDays(-30).Date);
Logger.Debug("Logs have been trimmed, events older than 30 days have been removed");
}
}
}