Fix #3625: add the clear logs function.
This commit is contained in:
parent
578b7b0512
commit
4c08a527be
|
@ -87,6 +87,7 @@ else
|
||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
<button type="button" class="btn btn-success" @onclick="SaveSiteSettings">@SharedLocalizer["Save"]</button>
|
<button type="button" class="btn btn-success" @onclick="SaveSiteSettings">@SharedLocalizer["Save"]</button>
|
||||||
|
<ActionDialog Header="Clear Logs" Message="@Localizer["Confirm.ClearLogs"]" Action="ClearLogs" Class="btn btn-secondary" OnClick="@(async () => await ClearLogs())" ResourceKey="ClearLogs" />
|
||||||
</TabPanel>
|
</TabPanel>
|
||||||
</TabStrip>
|
</TabStrip>
|
||||||
}
|
}
|
||||||
|
@ -230,6 +231,21 @@ else
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task ClearLogs()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await LogService.ClearLogsAsync(PageState.Site.SiteId);
|
||||||
|
await GetLogs();
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await logger.LogError(ex, "Error Clearing Logs {Error}", ex.Message);
|
||||||
|
AddModuleMessage(Localizer["Error.ClearLogs"], MessageType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void OnPageChange(int page)
|
private void OnPageChange(int page)
|
||||||
{
|
{
|
||||||
_page = page;
|
_page = page;
|
||||||
|
|
|
@ -210,4 +210,13 @@
|
||||||
<data name="Success.SaveSiteSettings" xml:space="preserve">
|
<data name="Success.SaveSiteSettings" xml:space="preserve">
|
||||||
<value>Settings Saved Successfully</value>
|
<value>Settings Saved Successfully</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="ClearLogs" xml:space="preserve">
|
||||||
|
<value>Clear Logs</value>
|
||||||
|
</data>
|
||||||
|
<data name="Confirm.ClearLogs" xml:space="preserve">
|
||||||
|
<value>Are you sure you wish to clear all the logs?</value>
|
||||||
|
</data>
|
||||||
|
<data name="Error.ClearLogs" xml:space="preserve">
|
||||||
|
<value>Error Clearing Logs</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -29,6 +29,13 @@ namespace Oqtane.Services
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<Log> GetLogAsync(int logId);
|
Task<Log> GetLogAsync(int logId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clear the entire logs of the given site.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="siteId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task ClearLogsAsync(int siteId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new log entry
|
/// Creates a new log entry
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -35,6 +35,11 @@ namespace Oqtane.Services
|
||||||
return await GetJsonAsync<Log>($"{Apiurl}/{logId}");
|
return await GetJsonAsync<Log>($"{Apiurl}/{logId}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task ClearLogsAsync(int siteId)
|
||||||
|
{
|
||||||
|
await DeleteAsync($"{Apiurl}?siteid={siteId}&includeErrors=true");
|
||||||
|
}
|
||||||
|
|
||||||
public async Task Log(int? pageId, int? moduleId, int? userId, string category, string feature, LogFunction function, LogLevel level, Exception exception, string message, params object[] args)
|
public async Task Log(int? pageId, int? moduleId, int? userId, string category, string feature, LogFunction function, LogLevel level, Exception exception, string message, params object[] args)
|
||||||
{
|
{
|
||||||
await Log(null, pageId, moduleId, userId, category, feature, function, level, exception, message, args);
|
await Log(null, pageId, moduleId, userId, category, feature, function, level, exception, message, args);
|
||||||
|
|
|
@ -76,5 +76,20 @@ namespace Oqtane.Controllers
|
||||||
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpDelete]
|
||||||
|
[Authorize(Roles = RoleNames.Admin)]
|
||||||
|
public void Delete(string siteId, bool includeErrors)
|
||||||
|
{
|
||||||
|
if (int.TryParse(siteId, out int parsedSiteId) && parsedSiteId == _alias.SiteId)
|
||||||
|
{
|
||||||
|
_logs.DeleteLogs(parsedSiteId, 0, includeErrors);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Log Delete Attempt {SiteId}", siteId);
|
||||||
|
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,6 @@ namespace Oqtane.Repository
|
||||||
IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows);
|
IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows);
|
||||||
Log GetLog(int logId);
|
Log GetLog(int logId);
|
||||||
void AddLog(Log log);
|
void AddLog(Log log);
|
||||||
int DeleteLogs(int siteId, int age);
|
int DeleteLogs(int siteId, int age, bool includeErrors = false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,20 +53,20 @@ namespace Oqtane.Repository
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int DeleteLogs(int siteId, int age)
|
public int DeleteLogs(int siteId, int age, bool includeErrors = false)
|
||||||
{
|
{
|
||||||
using var db = _dbContextFactory.CreateDbContext();
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
// delete logs in batches of 100 records
|
// delete logs in batches of 100 records
|
||||||
var count = 0;
|
var count = 0;
|
||||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||||
var logs = db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
var logs = db.Log.Where(item => item.SiteId == siteId && (includeErrors || item.Level != "Error") && item.LogDate < purgedate)
|
||||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||||
while (logs.Count > 0)
|
while (logs.Count > 0)
|
||||||
{
|
{
|
||||||
count += logs.Count;
|
count += logs.Count;
|
||||||
db.Log.RemoveRange(logs);
|
db.Log.RemoveRange(logs);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
logs = db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
logs = db.Log.Where(item => item.SiteId == siteId && (includeErrors || item.Level != "Error") && item.LogDate < purgedate)
|
||||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -35,6 +35,9 @@ app {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Action Dialog */
|
/* Action Dialog */
|
||||||
|
.app-actiondialog{
|
||||||
|
position: absolute;
|
||||||
|
}
|
||||||
.app-actiondialog .modal {
|
.app-actiondialog .modal {
|
||||||
position: fixed; /* Stay in place */
|
position: fixed; /* Stay in place */
|
||||||
z-index: 9999; /* Sit on top */
|
z-index: 9999; /* Sit on top */
|
||||||
|
|
Loading…
Reference in New Issue
Block a user