2011-09-05 21:59:39 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-11-23 06:58:26 +01:00
|
|
|
|
using System.Linq;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
using NLog;
|
2012-01-23 03:24:16 +01:00
|
|
|
|
using NzbDrone.Common;
|
2011-06-18 02:11:12 +02:00
|
|
|
|
using PetaPoco;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Instrumentation
|
|
|
|
|
{
|
2011-04-10 02:14:51 +02:00
|
|
|
|
public class LogProvider
|
2010-10-24 09:46:58 +02:00
|
|
|
|
{
|
2011-06-18 02:11:12 +02:00
|
|
|
|
private readonly IDatabase _database;
|
2011-11-23 06:58:26 +01:00
|
|
|
|
private readonly LogDbContext _logDbContext;
|
2012-01-23 03:24:16 +01:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
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)
|
2010-10-24 09:46:58 +02:00
|
|
|
|
{
|
2011-06-18 02:11:12 +02:00
|
|
|
|
_database = database;
|
2011-11-23 06:58:26 +01:00
|
|
|
|
_logDbContext = logDbContext;
|
2012-01-23 03:24:16 +01:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_enviromentProvider = enviromentProvider;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
public IQueryable<Log> GetAllLogs()
|
2010-10-24 09:46:58 +02:00
|
|
|
|
{
|
2011-11-23 06:58:26 +01:00
|
|
|
|
return _logDbContext.Logs;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 02:01:51 +02:00
|
|
|
|
public IList<Log> TopLogs(int count)
|
2011-09-05 21:59:39 +02:00
|
|
|
|
{
|
2011-09-08 02:01:51 +02:00
|
|
|
|
var logs = _database.Fetch<Log>("SELECT TOP " + count + " * FROM Logs ORDER BY Time Desc");
|
2011-09-05 21:59:39 +02:00
|
|
|
|
logs.Add(new Log
|
|
|
|
|
{
|
|
|
|
|
Time = DateTime.Now.AddYears(-100),
|
|
|
|
|
Level = "Info",
|
2011-09-08 02:01:51 +02:00
|
|
|
|
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))
|
2011-09-05 21:59:39 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return logs;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 08:41:50 +02:00
|
|
|
|
public Page<Log> GetPagedLogs(int pageNumber, int pageSize)
|
|
|
|
|
{
|
|
|
|
|
return _database.Page<Log>(pageNumber, pageSize, "SELECT * FROM Logs ORDER BY Time DESC");
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
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());
|
2010-10-24 09:46:58 +02:00
|
|
|
|
Logger.Info("Cleared Log History");
|
|
|
|
|
}
|
2011-11-22 08:35:11 +01:00
|
|
|
|
|
|
|
|
|
public void Trim()
|
|
|
|
|
{
|
|
|
|
|
_database.Delete<Log>("WHERE Time < @0", DateTime.Now.AddDays(-30).Date);
|
2011-11-23 06:58:26 +01:00
|
|
|
|
Logger.Debug("Logs have been trimmed, events older than 30 days have been removed");
|
2011-11-22 08:35:11 +01:00
|
|
|
|
}
|
2010-10-24 09:46:58 +02:00
|
|
|
|
}
|
2011-04-10 04:44:01 +02:00
|
|
|
|
}
|