2018-09-14 15:17:12 +01:00
|
|
|
|
using System;
|
2018-08-28 18:30:58 +01:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-26 19:05:15 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-05-20 16:54:20 +02:00
|
|
|
|
using Umbraco.Core;
|
2018-08-27 20:05:59 +01:00
|
|
|
|
using Umbraco.Core.Logging.Viewer;
|
2018-08-28 18:30:58 +01:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-05-26 19:05:15 +02:00
|
|
|
|
using Umbraco.Web.Common.Attributes;
|
|
|
|
|
|
using Umbraco.Web.Common.Exceptions;
|
|
|
|
|
|
using Umbraco.Web.Editors;
|
2018-08-27 20:05:59 +01:00
|
|
|
|
|
2020-05-26 19:05:15 +02:00
|
|
|
|
namespace Umbraco.Web.BackOffice.Controllers
|
2018-08-27 20:05:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-08-28 14:48:54 +01:00
|
|
|
|
/// Backoffice controller supporting the dashboard for viewing logs with some simple graphs & filtering
|
2018-08-27 20:05:59 +01:00
|
|
|
|
/// </summary>
|
2020-06-09 13:01:05 +10:00
|
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2018-08-29 18:42:12 +01:00
|
|
|
|
public class LogViewerController : UmbracoAuthorizedJsonController
|
2018-08-27 20:05:59 +01:00
|
|
|
|
{
|
2019-07-04 14:35:06 +02:00
|
|
|
|
private readonly ILogViewer _logViewer;
|
2018-08-27 20:05:59 +01:00
|
|
|
|
|
2018-08-29 18:42:12 +01:00
|
|
|
|
public LogViewerController(ILogViewer logViewer)
|
2018-08-27 20:05:59 +01:00
|
|
|
|
{
|
2019-07-04 14:35:06 +02:00
|
|
|
|
_logViewer = logViewer ?? throw new ArgumentNullException(nameof(logViewer));
|
2018-08-27 20:05:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-03 14:56:44 +02:00
|
|
|
|
private bool CanViewLogs(LogTimePeriod logTimePeriod)
|
2018-11-01 16:14:03 +00:00
|
|
|
|
{
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//Can the interface deal with Large Files
|
|
|
|
|
|
if (_logViewer.CanHandleLargeLogs)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//Interface CheckCanOpenLogs
|
2019-05-03 14:56:44 +02:00
|
|
|
|
return _logViewer.CheckCanOpenLogs(logTimePeriod);
|
2018-11-01 16:24:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-05-26 19:05:15 +02:00
|
|
|
|
public bool GetCanViewLogs([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
{
|
2019-05-03 14:56:44 +02:00
|
|
|
|
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
|
|
|
|
|
return CanViewLogs(logTimePeriod);
|
2018-11-01 16:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-27 20:05:59 +01:00
|
|
|
|
[HttpGet]
|
2020-05-26 19:05:15 +02:00
|
|
|
|
public int GetNumberOfErrors([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
2018-08-27 20:05:59 +01:00
|
|
|
|
{
|
2019-05-03 14:56:44 +02:00
|
|
|
|
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//We will need to stop the request if trying to do this on a 1GB file
|
2019-05-03 14:56:44 +02:00
|
|
|
|
if (CanViewLogs(logTimePeriod) == false)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
{
|
2020-05-26 19:05:15 +02:00
|
|
|
|
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
2018-11-01 16:24:48 +00:00
|
|
|
|
}
|
2018-11-01 16:19:18 +00:00
|
|
|
|
|
2019-05-03 14:56:44 +02:00
|
|
|
|
return _logViewer.GetNumberOfErrors(logTimePeriod);
|
2018-08-27 20:05:59 +01:00
|
|
|
|
}
|
2018-09-14 15:17:12 +01:00
|
|
|
|
|
2018-08-27 20:05:59 +01:00
|
|
|
|
[HttpGet]
|
2020-05-26 19:05:15 +02:00
|
|
|
|
public LogLevelCounts GetLogLevelCounts([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
2018-08-27 20:05:59 +01:00
|
|
|
|
{
|
2019-05-03 14:56:44 +02:00
|
|
|
|
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//We will need to stop the request if trying to do this on a 1GB file
|
2019-05-03 14:56:44 +02:00
|
|
|
|
if (CanViewLogs(logTimePeriod) == false)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
{
|
2020-05-26 19:05:15 +02:00
|
|
|
|
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
2018-11-01 16:24:48 +00:00
|
|
|
|
}
|
2018-11-01 16:19:18 +00:00
|
|
|
|
|
2019-05-03 14:56:44 +02:00
|
|
|
|
return _logViewer.GetLogLevelCounts(logTimePeriod);
|
2018-08-27 20:05:59 +01:00
|
|
|
|
}
|
2018-08-28 14:48:54 +01:00
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-05-26 19:05:15 +02:00
|
|
|
|
public IEnumerable<LogTemplate> GetMessageTemplates([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
2018-08-28 14:48:54 +01:00
|
|
|
|
{
|
2019-05-03 14:56:44 +02:00
|
|
|
|
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//We will need to stop the request if trying to do this on a 1GB file
|
2019-05-03 14:56:44 +02:00
|
|
|
|
if (CanViewLogs(logTimePeriod) == false)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
{
|
2020-05-26 19:05:15 +02:00
|
|
|
|
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
2018-11-01 16:24:48 +00:00
|
|
|
|
}
|
2018-11-01 16:19:18 +00:00
|
|
|
|
|
2019-05-03 14:56:44 +02:00
|
|
|
|
return _logViewer.GetMessageTemplates(logTimePeriod);
|
2018-08-28 14:48:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-05-26 19:05:15 +02:00
|
|
|
|
public PagedResult<LogMessage> GetLogs(string orderDirection = "Descending", int pageNumber = 1, string filterExpression = null, [FromQuery]string[] logLevels = null, [FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
2018-08-28 14:48:54 +01:00
|
|
|
|
{
|
2019-05-03 14:56:44 +02:00
|
|
|
|
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
|
|
|
|
|
|
2018-11-02 11:09:59 +00:00
|
|
|
|
//We will need to stop the request if trying to do this on a 1GB file
|
2019-05-03 14:56:44 +02:00
|
|
|
|
if (CanViewLogs(logTimePeriod) == false)
|
2018-11-01 16:24:48 +00:00
|
|
|
|
{
|
2020-05-26 19:05:15 +02:00
|
|
|
|
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
2018-11-01 16:24:48 +00:00
|
|
|
|
}
|
2018-11-01 16:19:18 +00:00
|
|
|
|
|
2018-08-30 14:27:31 +01:00
|
|
|
|
var direction = orderDirection == "Descending" ? Direction.Descending : Direction.Ascending;
|
2019-05-03 14:56:44 +02:00
|
|
|
|
|
|
|
|
|
|
return _logViewer.GetLogs(logTimePeriod, filterExpression: filterExpression, pageNumber: pageNumber, orderDirection: direction, logLevels: logLevels);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static LogTimePeriod GetTimePeriod(DateTime? startDate, DateTime? endDate)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (startDate == null || endDate == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
|
if (startDate == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
startDate = now.AddDays(-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (endDate == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
endDate = now;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new LogTimePeriod(startDate.Value, endDate.Value);
|
2018-08-28 14:48:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-12 11:35:51 +01:00
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public IEnumerable<SavedLogSearch> GetSavedSearches()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _logViewer.GetSavedSearches();
|
|
|
|
|
|
}
|
2018-09-12 16:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public IEnumerable<SavedLogSearch> PostSavedSearch(SavedLogSearch item)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _logViewer.AddSavedSearch(item.Name, item.Query);
|
|
|
|
|
|
}
|
2018-09-14 15:17:12 +01:00
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public IEnumerable<SavedLogSearch> DeleteSavedSearch(SavedLogSearch item)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _logViewer.DeleteSavedSearch(item.Name, item.Query);
|
|
|
|
|
|
}
|
2019-08-14 06:07:05 +10:00
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public string GetLogLevel()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _logViewer.GetLogLevel();
|
|
|
|
|
|
}
|
2018-08-27 20:05:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|