Merge pull request #4099 from zyhfish/task/fix-3625

Fix #3625: add the clear logs function.
This commit is contained in:
Shaun Walker 2024-04-04 08:26:41 -04:00 committed by GitHub
commit 160b3ff655
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 115 additions and 45 deletions

View File

@ -87,6 +87,7 @@ else
</div>
<br />
<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>
</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)
{
_page = page;

View File

@ -210,4 +210,13 @@
<data name="Success.SaveSiteSettings" xml:space="preserve">
<value>Settings Saved Successfully</value>
</data>
<data name="ClearLogs.Text" 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>

View File

@ -29,6 +29,13 @@ namespace Oqtane.Services
/// <returns></returns>
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>
/// Creates a new log entry
/// </summary>

View File

@ -35,6 +35,11 @@ namespace Oqtane.Services
return await GetJsonAsync<Log>($"{Apiurl}/{logId}");
}
public async Task ClearLogsAsync(int siteId)
{
await DeleteAsync($"{Apiurl}?siteid={siteId}");
}
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);

View File

@ -76,5 +76,20 @@ namespace Oqtane.Controllers
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
[HttpDelete]
[Authorize(Roles = RoleNames.Admin)]
public void Delete(string siteId)
{
if (int.TryParse(siteId, out int parsedSiteId) && parsedSiteId == _alias.SiteId)
{
_logs.ClearLogs(parsedSiteId);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Log Delete Attempt {SiteId}", siteId);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
}
}

View File

@ -9,5 +9,6 @@ namespace Oqtane.Repository
Log GetLog(int logId);
void AddLog(Log log);
int DeleteLogs(int siteId, int age);
void ClearLogs(int siteId);
}
}

View File

@ -71,5 +71,19 @@ namespace Oqtane.Repository
}
return count;
}
public void ClearLogs(int siteId)
{
using var db = _dbContextFactory.CreateDbContext();
var getLogsForDelete = () => db.Log.Where(item => item.SiteId == siteId).Take(100).ToList();
// delete logs in batches of 100 records
var logs = getLogsForDelete();
while (logs.Count > 0)
{
db.Log.RemoveRange(logs);
db.SaveChanges();
logs = getLogsForDelete();
}
}
}
}

View File

@ -35,6 +35,9 @@ app {
}
/* Action Dialog */
.app-actiondialog{
position: absolute;
}
.app-actiondialog .modal {
position: fixed; /* Stay in place */
z-index: 9999; /* Sit on top */