https://dev.azure.com/umbraco/D-Team%20Tracker/_workitems/edit/6587 - LogViewerController
This commit is contained in:
141
src/Umbraco.Web.BackOffice/Controllers/LogViewerController.cs
Normal file
141
src/Umbraco.Web.BackOffice/Controllers/LogViewerController.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Logging.Viewer;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
using Umbraco.Web.Common.Exceptions;
|
||||
using Umbraco.Web.Editors;
|
||||
|
||||
namespace Umbraco.Web.BackOffice.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Backoffice controller supporting the dashboard for viewing logs with some simple graphs & filtering
|
||||
/// </summary>
|
||||
[PluginController("UmbracoApi")]
|
||||
public class LogViewerController : UmbracoAuthorizedJsonController
|
||||
{
|
||||
private readonly ILogViewer _logViewer;
|
||||
|
||||
public LogViewerController(ILogViewer logViewer)
|
||||
{
|
||||
_logViewer = logViewer ?? throw new ArgumentNullException(nameof(logViewer));
|
||||
}
|
||||
|
||||
private bool CanViewLogs(LogTimePeriod logTimePeriod)
|
||||
{
|
||||
//Can the interface deal with Large Files
|
||||
if (_logViewer.CanHandleLargeLogs)
|
||||
return true;
|
||||
|
||||
//Interface CheckCanOpenLogs
|
||||
return _logViewer.CheckCanOpenLogs(logTimePeriod);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public bool GetCanViewLogs([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
||||
{
|
||||
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
||||
return CanViewLogs(logTimePeriod);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public int GetNumberOfErrors([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
||||
{
|
||||
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs(logTimePeriod) == false)
|
||||
{
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
||||
}
|
||||
|
||||
return _logViewer.GetNumberOfErrors(logTimePeriod);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public LogLevelCounts GetLogLevelCounts([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
||||
{
|
||||
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs(logTimePeriod) == false)
|
||||
{
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
||||
}
|
||||
|
||||
return _logViewer.GetLogLevelCounts(logTimePeriod);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<LogTemplate> GetMessageTemplates([FromQuery] DateTime? startDate = null,[FromQuery] DateTime? endDate = null)
|
||||
{
|
||||
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs(logTimePeriod) == false)
|
||||
{
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
||||
}
|
||||
|
||||
return _logViewer.GetMessageTemplates(logTimePeriod);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
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)
|
||||
{
|
||||
var logTimePeriod = GetTimePeriod(startDate, endDate);
|
||||
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs(logTimePeriod) == false)
|
||||
{
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse("Unable to view logs, due to size");
|
||||
}
|
||||
|
||||
var direction = orderDirection == "Descending" ? Direction.Descending : Direction.Ascending;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<SavedLogSearch> GetSavedSearches()
|
||||
{
|
||||
return _logViewer.GetSavedSearches();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IEnumerable<SavedLogSearch> PostSavedSearch(SavedLogSearch item)
|
||||
{
|
||||
return _logViewer.AddSavedSearch(item.Name, item.Query);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IEnumerable<SavedLogSearch> DeleteSavedSearch(SavedLogSearch item)
|
||||
{
|
||||
return _logViewer.DeleteSavedSearch(item.Name, item.Query);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public string GetLogLevel()
|
||||
{
|
||||
return _logViewer.GetLogLevel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user