1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 07:52:37 +01:00
Sonarr/NzbDrone.Core/Instrumentation/LogProvider.cs

66 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
2012-01-23 03:24:16 +01:00
using NzbDrone.Common;
2011-06-18 02:11:12 +02:00
using PetaPoco;
namespace NzbDrone.Core.Instrumentation
{
public class LogProvider
{
2011-06-18 02:11:12 +02:00
private readonly IDatabase _database;
private readonly LogDbContext _logDbContext;
2012-01-23 03:24:16 +01:00
private readonly DiskProvider _diskProvider;
private readonly EnviromentProvider _enviromentProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-06-18 02:11:12 +02:00
2012-01-23 03:24:16 +01:00
public LogProvider(IDatabase database, LogDbContext logDbContext, DiskProvider diskProvider, EnviromentProvider enviromentProvider)
{
2011-06-18 02:11:12 +02:00
_database = database;
_logDbContext = logDbContext;
2012-01-23 03:24:16 +01:00
_diskProvider = diskProvider;
_enviromentProvider = enviromentProvider;
}
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()
{
2011-06-18 02:11:12 +02:00
_database.Delete<Log>("");
2012-01-23 03:24:16 +01:00
_diskProvider.DeleteFile(_enviromentProvider.GetLogFileName());
_diskProvider.DeleteFile(_enviromentProvider.GetArchivedLogFileName());
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");
}
}
2011-04-10 04:44:01 +02:00
}