Sonarr/NzbDrone.Web/Controllers/LogController.cs

108 lines
3.8 KiB
C#
Raw Normal View History

2012-01-23 02:24:16 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
2012-01-23 02:24:16 +00:00
using System.Text;
using System.Web.Mvc;
using DataTables.Mvc.Core;
using DataTables.Mvc.Core.Models;
2012-01-23 02:24:16 +00:00
using NzbDrone.Common;
using NzbDrone.Core.Instrumentation;
2011-08-22 01:00:12 +00:00
using NzbDrone.Web.Models;
namespace NzbDrone.Web.Controllers
{
public class LogController : Controller
{
private readonly LogProvider _logProvider;
private readonly EnvironmentProvider _environmentProvider;
2012-01-23 02:24:16 +00:00
private readonly DiskProvider _diskProvider;
2012-04-21 08:16:15 +00:00
public LogController(LogProvider logProvider, EnvironmentProvider environmentProvider,
DiskProvider diskProvider)
{
_logProvider = logProvider;
_environmentProvider = environmentProvider;
2012-01-23 02:24:16 +00:00
_diskProvider = diskProvider;
}
public ActionResult Index()
{
return View();
}
2012-01-23 02:24:16 +00:00
public FileContentResult File()
{
string log = string.Empty;
if (_diskProvider.FileExists(_environmentProvider.GetArchivedLogFileName()))
2012-01-23 02:24:16 +00:00
{
log = _diskProvider.ReadAllText(_environmentProvider.GetArchivedLogFileName());
2012-01-23 02:24:16 +00:00
}
log += _diskProvider.ReadAllText(_environmentProvider.GetLogFileName());
2012-01-23 02:24:16 +00:00
return new FileContentResult(Encoding.ASCII.GetBytes(log), "text/plain");
}
public JsonResult Clear()
{
_logProvider.DeleteAll();
return JsonNotificationResult.Info("Logs Cleared");
}
public ActionResult AjaxBinding(DataTablesParams dataTablesParams)
{
var logs = _logProvider.GetAllLogs();
var totalCount = logs.Count();
IQueryable<Log> q = logs;
if (!string.IsNullOrEmpty(dataTablesParams.sSearch))
{
q = q.Where(b => b.Logger.Contains(dataTablesParams.sSearch)
|| b.Exception.Contains(dataTablesParams.sSearch)
|| b.Message.Contains(dataTablesParams.sSearch));
}
int filteredCount = q.Count();
IQueryable<Log> sorted = q;
for (int i = 0; i < dataTablesParams.iSortingCols; i++)
{
int sortCol = dataTablesParams.iSortCol[i];
var sortColName = sortCol == 0 ? "Time" : sortCol == 1 ? "Level" : "Logger";
var sortExpression = String.Format("{0} {1}", sortColName, dataTablesParams.sSortDir[i]);
sorted = sorted.OrderBy(sortExpression);
}
IQueryable<Log> filteredAndSorted = sorted;
if (filteredCount > dataTablesParams.iDisplayLength)
{
filteredAndSorted = sorted.Skip(dataTablesParams.iDisplayStart).Take(dataTablesParams.iDisplayLength);
}
var logModels = filteredAndSorted.ToList().Select(s => new LogModel
{
Time = s.Time.ToString(),
Level = s.Level,
Source = s.Logger,
Message = s.Message,
Method = s.Method,
ExceptionType = s.ExceptionType,
Exception = s.Exception
});
return Json(new
{
sEcho = dataTablesParams.sEcho,
iTotalRecords = totalCount,
iTotalDisplayRecords = filteredCount,
aaData = logModels
},
JsonRequestBehavior.AllowGet);
}
}
2011-04-10 02:44:01 +00:00
}