Adds in a new API endpoint to get the size of logs - which will determine to display a warning message if the filesize is too big to read (to help prevent the CPU & memory being eaten up & killing the site)

This commit is contained in:
Warren Buckley
2018-11-01 16:14:03 +00:00
parent ef30c1f005
commit 24a6aecd73
4 changed files with 74 additions and 3 deletions

View File

@@ -44,6 +44,8 @@ namespace Umbraco.Core.Logging.Viewer
/// </summary>
IEnumerable<LogTemplate> GetMessageTemplates(DateTimeOffset startDate, DateTimeOffset endDate);
long GetLogSize(DateTimeOffset startDate, DateTimeOffset endDate);
/// <summary>
/// Returns the collection of logs
/// </summary>

View File

@@ -67,6 +67,45 @@ namespace Umbraco.Core.Logging.Viewer
}
return logs;
}
}
/// <summary>
/// This default JSON disk implementation here - returns the total filesize & NOT the count of entries
/// Other implementations we would expect to return the count of entries
/// We use this number to help prevent the logviewer killing the site with CPU/Memory if the number of items too big to handle
/// </summary>
public override long GetLogSize(DateTimeOffset startDate, DateTimeOffset endDate)
{
//Open the JSON log file for the range of dates (and exclude machinename) Could be several for LB
var dateRange = endDate - startDate;
//Log Directory
var logDirectory = $@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\";
//Number of entries
long count = 0;
//foreach full day in the range - see if we can find one or more filenames that end with
//yyyyMMdd.json - Ends with due to MachineName in filenames - could be 1 or more due to load balancing
for (var day = startDate.Date; day.Date <= endDate.Date; day = day.AddDays(1))
{
//Filename ending to search for (As could be multiple)
var filesToFind = $"*{day.ToString("yyyyMMdd")}.json";
var filesForCurrentDay = Directory.GetFiles(logDirectory, filesToFind);
//Foreach file we find - open it
foreach (var filePath in filesForCurrentDay)
{
//Get the current filesize in bytes !
var byteFileSize = new FileInfo(filePath).Length;
count += byteFileSize;
}
}
//Count contains a combination of file sizes in bytes
return count;
}
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Serilog.Events;
using Serilog.Filters.Expressions;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
@@ -17,6 +16,8 @@ namespace Umbraco.Core.Logging.Viewer
public abstract IEnumerable<LogEvent> GetLogs(DateTimeOffset startDate, DateTimeOffset endDate, ILogFilter filter, int skip, int take);
public abstract long GetLogSize(DateTimeOffset startDate, DateTimeOffset endDate);
public virtual IEnumerable<SavedLogSearch> GetSavedSearches()
{
//Our default implementation
@@ -137,6 +138,6 @@ namespace Umbraco.Core.Logging.Viewer
{
Items = logMessages
};
}
}
}
}

View File

@@ -21,6 +21,35 @@ namespace Umbraco.Web.Editors
_logViewer = logViewer;
}
[HttpGet]
public IHttpActionResult GetLogSize()
{
//Returns 200 OK if the logs can be viewed
//Check if the ILogViewer is our JSON file
var isJsonLogViewer = _logViewer is JsonLogViewer;
//Don't WARN or check if it's not our JSON disk file approach
if (isJsonLogViewer == false)
{
return Ok();
}
//Go & fetch the number of log entries OR
var logSize = _logViewer.GetLogSize(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
//If the number of items is less than
if (logSize >= 10)
{
return Ok(logSize);
}
//TODO: It may need to be an Umbraco request with errow/warning notification?!
//Depends how best to bubble up to UI - with some custom JS promise error that is caught
return BadRequest();
}
[HttpGet]
public int GetNumberOfErrors()
{