Radarr/NzbDrone.Core/Instrumentation/LogProvider.cs

90 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2013-01-08 08:57:38 +00:00
using DataTables.Mvc.Core.Helpers;
using DataTables.Mvc.Core.Models;
using NLog;
2012-01-23 02:24:16 +00:00
using NzbDrone.Common;
2011-06-18 00:11:12 +00:00
using PetaPoco;
namespace NzbDrone.Core.Instrumentation
{
public class LogProvider
{
2011-06-18 00:11:12 +00:00
private readonly IDatabase _database;
2012-01-23 02:24:16 +00:00
private readonly DiskProvider _diskProvider;
private readonly EnvironmentProvider _environmentProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2013-01-08 08:57:38 +00:00
public LogProvider(IDatabase database, DiskProvider diskProvider, EnvironmentProvider environmentProvider)
{
2011-06-18 00:11:12 +00:00
_database = database;
2012-01-23 02:24:16 +00:00
_diskProvider = diskProvider;
_environmentProvider = environmentProvider;
}
2013-01-08 08:57:38 +00:00
public List<Log> GetAllLogs()
{
2013-01-08 08:57:38 +00:00
return _database.Fetch<Log>();
}
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;
}
2013-01-08 08:57:38 +00:00
public virtual Page<Log> GetPagedItems(DataTablesPageRequest pageRequest)
{
var query = Sql.Builder
.Select(@"*")
.From("Logs");
var startPage = (pageRequest.DisplayLength == 0) ? 1 : pageRequest.DisplayStart / pageRequest.DisplayLength + 1;
if (!string.IsNullOrEmpty(pageRequest.Search))
{
var whereClause = string.Join(" OR ", SqlBuilderHelper.GetSearchClause(pageRequest));
if (!string.IsNullOrEmpty(whereClause))
query.Append("WHERE " + whereClause, "%" + pageRequest.Search + "%");
}
var orderBy = string.Join(",", SqlBuilderHelper.GetOrderByClause(pageRequest));
if (!string.IsNullOrEmpty(orderBy))
{
query.Append("ORDER BY " + orderBy);
}
return _database.Page<Log>(startPage, pageRequest.DisplayLength, query);
}
public virtual long Count()
{
2013-01-08 08:57:38 +00:00
return _database.Single<long>(@"SELECT COUNT(*) from Logs");
}
public void DeleteAll()
{
2011-06-18 00:11:12 +00:00
_database.Delete<Log>("");
_diskProvider.DeleteFile(_environmentProvider.GetLogFileName());
_diskProvider.DeleteFile(_environmentProvider.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 02:44:01 +00:00
}