Update logic for pre-flight check
This commit is contained in:
@@ -44,7 +44,9 @@ namespace Umbraco.Core.Logging.Viewer
|
||||
/// </summary>
|
||||
IEnumerable<LogTemplate> GetMessageTemplates(DateTimeOffset startDate, DateTimeOffset endDate);
|
||||
|
||||
long GetLogSize(DateTimeOffset startDate, DateTimeOffset endDate);
|
||||
bool CanHandleLargeLogs { get; }
|
||||
|
||||
bool CheckCanOpenLogs(DateTimeOffset startDate, DateTimeOffset endDate);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the collection of logs
|
||||
|
||||
@@ -7,7 +7,47 @@ using Serilog.Formatting.Compact.Reader;
|
||||
namespace Umbraco.Core.Logging.Viewer
|
||||
{
|
||||
public partial class JsonLogViewer : LogViewerSourceBase
|
||||
{
|
||||
{
|
||||
const int FileSizeCap = 100;
|
||||
|
||||
public override bool CanHandleLargeLogs { get => false; }
|
||||
|
||||
public override bool CheckCanOpenLogs(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 fileSizeCount = 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;
|
||||
|
||||
fileSizeCount += byteFileSize;
|
||||
}
|
||||
}
|
||||
|
||||
//The GetLogSize call on JsonLogViewer returns the total filesize in bytes
|
||||
//Check if the logsize is not greater than 100Mb (FileSizeCap)
|
||||
var logSizeAsMegabytes = fileSizeCount / 1024 / 1024;
|
||||
return logSizeAsMegabytes <= FileSizeCap;
|
||||
}
|
||||
|
||||
public override IEnumerable<LogEvent> GetLogs(DateTimeOffset startDate, DateTimeOffset endDate, ILogFilter filter, int skip, int take)
|
||||
{
|
||||
var logs = new List<LogEvent>();
|
||||
@@ -68,44 +108,6 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,11 @@ namespace Umbraco.Core.Logging.Viewer
|
||||
|
||||
private static readonly string SearchesConfigPath = IOHelper.MapPath("~/Config/logviewer.searches.config.js");
|
||||
|
||||
public abstract bool CanHandleLargeLogs { get; }
|
||||
|
||||
public abstract IEnumerable<LogEvent> GetLogs(DateTimeOffset startDate, DateTimeOffset endDate, ILogFilter filter, int skip, int take);
|
||||
|
||||
public abstract long GetLogSize(DateTimeOffset startDate, DateTimeOffset endDate);
|
||||
|
||||
public abstract bool CheckCanOpenLogs(DateTimeOffset startDate, DateTimeOffset endDate);
|
||||
|
||||
public virtual IEnumerable<SavedLogSearch> GetSavedSearches()
|
||||
{
|
||||
@@ -139,5 +141,7 @@ namespace Umbraco.Core.Logging.Viewer
|
||||
Items = logMessages
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,15 @@
|
||||
"GetLogs",
|
||||
options)),
|
||||
'Failed to retrieve common log messages');
|
||||
},
|
||||
|
||||
canViewLogs: function () {
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"logViewerApiBaseUrl",
|
||||
"GetCanViewLogs")),
|
||||
'Failed to retrieve state if logs can be viewed');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
var vm = this;
|
||||
vm.loading = false;
|
||||
vm.canLoadLogs = false;
|
||||
vm.searches = [];
|
||||
vm.numberOfErrors = 0;
|
||||
vm.commonLogMessages = [];
|
||||
@@ -25,6 +26,22 @@
|
||||
vm.searchLogQuery = searchLogQuery;
|
||||
vm.findMessageTemplate = findMessageTemplate;
|
||||
|
||||
function preFlightCheck(){
|
||||
vm.loading = true;
|
||||
|
||||
//Do our pre-flight check (to see if we can view logs)
|
||||
//IE the log file is NOT too big such as 1GB & crash the site
|
||||
logViewerResource.canViewLogs().then(function(result){
|
||||
vm.loading = false;
|
||||
vm.canLoadLogs = result;
|
||||
|
||||
if(result){
|
||||
//Can view logs - so initalise
|
||||
init();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function init() {
|
||||
|
||||
@@ -96,7 +113,7 @@
|
||||
searchLogQuery(logQuery);
|
||||
}
|
||||
|
||||
init();
|
||||
preFlightCheck();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,18 @@
|
||||
<umb-editor-container>
|
||||
<umb-load-indicator ng-if="vm.loading"></umb-load-indicator>
|
||||
|
||||
<div class="umb-package-details" ng-if="!vm.loading">
|
||||
<!-- Warning message (if unable to view log files) -->
|
||||
<div ng-show="!vm.loading && !vm.canLoadLogs">
|
||||
<umb-box>
|
||||
<umb-box-header title="Unable to view logs"/>
|
||||
<umb-box-content>
|
||||
<p>Today's log file is too large to be viewed and would cause performance problems.</p>
|
||||
<p>If you need to view the log files, try opening them manually</p>
|
||||
</umb-box-content>
|
||||
</umb-box>
|
||||
</div>
|
||||
|
||||
<div class="umb-package-details" ng-show="!vm.loading && vm.canLoadLogs">
|
||||
<div class="umb-package-details__main-content">
|
||||
<!-- Saved Searches -->
|
||||
<umb-box>
|
||||
|
||||
@@ -159,6 +159,8 @@
|
||||
vm.logsLoading = false;
|
||||
|
||||
setLogTypeColor(vm.logItems.items);
|
||||
}, function(err){
|
||||
vm.logsLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using Umbraco.Core.Logging.Viewer;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.WebApi;
|
||||
|
||||
namespace Umbraco.Web.Editors
|
||||
{
|
||||
@@ -23,53 +24,27 @@ namespace Umbraco.Web.Editors
|
||||
|
||||
private bool CanViewLogs()
|
||||
{
|
||||
//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)
|
||||
{
|
||||
//Can the interface deal with Large Files
|
||||
if (_logViewer.CanHandleLargeLogs)
|
||||
return true;
|
||||
}
|
||||
|
||||
//Go & fetch the number of log entries OR
|
||||
var logSize = _logViewer.GetLogSize(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
|
||||
|
||||
//The GetLogSize call on JsonLogViewer returns the total filesize in bytes
|
||||
//Check if the logsize is not greater than 200Mb
|
||||
//TODO: Convert the bytes to Megabytes and check less than 200Mb
|
||||
if (logSize >= 10)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//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 false;
|
||||
//Interface CheckCanOpenLogs
|
||||
return _logViewer.CheckCanOpenLogs(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IHttpActionResult GetCanViewLogs()
|
||||
public bool GetCanViewLogs()
|
||||
{
|
||||
//Returns 200 OK if the logs can be viewed
|
||||
if (CanViewLogs() == true)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
//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();
|
||||
|
||||
return CanViewLogs();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public int GetNumberOfErrors()
|
||||
{
|
||||
//TODO: We will need to stop the request if trying to do this on a 1GB file
|
||||
if(CanViewLogs() == false)
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs() == false)
|
||||
{
|
||||
//Throw err
|
||||
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("Unable to view logs, due to size"));
|
||||
}
|
||||
|
||||
return _logViewer.GetNumberOfErrors(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
|
||||
@@ -78,10 +53,10 @@ namespace Umbraco.Web.Editors
|
||||
[HttpGet]
|
||||
public LogLevelCounts GetLogLevelCounts()
|
||||
{
|
||||
//TODO: We will need to stop the request if trying to do this on a 1GB file
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs() == false)
|
||||
{
|
||||
//Throw err
|
||||
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("Unable to view logs, due to size"));
|
||||
}
|
||||
|
||||
return _logViewer.GetLogLevelCounts(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
|
||||
@@ -90,10 +65,10 @@ namespace Umbraco.Web.Editors
|
||||
[HttpGet]
|
||||
public IEnumerable<LogTemplate> GetMessageTemplates()
|
||||
{
|
||||
//TODO: We will need to stop the request if trying to do this on a 1GB file
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs() == false)
|
||||
{
|
||||
//Throw err
|
||||
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("Unable to view logs, due to size"));
|
||||
}
|
||||
|
||||
return _logViewer.GetMessageTemplates(startDate: DateTime.Now.AddDays(-1), endDate: DateTime.Now);
|
||||
@@ -102,10 +77,10 @@ namespace Umbraco.Web.Editors
|
||||
[HttpGet]
|
||||
public PagedResult<LogMessage> GetLogs(string orderDirection = "Descending", int pageNumber = 1, string filterExpression = null, [FromUri]string[] logLevels = null)
|
||||
{
|
||||
//TODO: We will need to stop the request if trying to do this on a 1GB file
|
||||
//We will need to stop the request if trying to do this on a 1GB file
|
||||
if (CanViewLogs() == false)
|
||||
{
|
||||
//Throw err
|
||||
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("Unable to view logs, due to size"));
|
||||
}
|
||||
|
||||
var direction = orderDirection == "Descending" ? Direction.Descending : Direction.Ascending;
|
||||
|
||||
Reference in New Issue
Block a user