Audit service rework (#19346)

* Introduce new AuditEntryService

- Moved logic related to the IAuditEntryRepository from the AuditService to the new service
- Introduced new Async methods
  - Using ids (for easier transition from the previous Write method)
  - Using keys
- Moved and updated integration tests related to the audit entries to a new test class `AuditEntryServiceTests`
- Added unit tests class `AuditEntryServiceTests` and added a few unit tests
- Added migration to add columns for `performingUserKey` and `affectedUserKey` and convert existing user ids
- Adjusted usages of the old AuditService.Write method to use the new one (mostly notification handlers)

* Audit service rework

- Added new async and paged methods
- Marked (now) redundant methods as obsolete
- Updated all of the usages to use the non-obsolete methods
- Added unit tests class `AuditServiceTests` and some unit tests
- Updated existing integration test

* Apply suggestions from code review

* Small improvement

* Update src/Umbraco.Core/Services/AuditService.cs

* Some minor adjustments following the merge

* Delete unnecessary file

* Small cleanup on the tests
This commit is contained in:
Laura Neto
2025-07-01 09:12:37 +02:00
committed by GitHub
parent ceb745a7bd
commit 447cb881bd
13 changed files with 836 additions and 323 deletions

View File

@@ -21,18 +21,17 @@ namespace Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs;
/// </remarks>
public class LogScrubberJob : IRecurringBackgroundJob
{
public TimeSpan Period { get => TimeSpan.FromHours(4); }
// No-op event as the period never changes on this job
public event EventHandler PeriodChanged { add { } remove { } }
private readonly IAuditService _auditService;
private readonly ILogger<LogScrubberJob> _logger;
private readonly IProfilingLogger _profilingLogger;
private readonly ICoreScopeProvider _scopeProvider;
private LoggingSettings _settings;
public TimeSpan Period => TimeSpan.FromHours(4);
// No-op event as the period never changes on this job
public event EventHandler PeriodChanged { add { } remove { } }
/// <summary>
/// Initializes a new instance of the <see cref="LogScrubberJob" /> class.
/// </summary>
@@ -48,7 +47,6 @@ public class LogScrubberJob : IRecurringBackgroundJob
ILogger<LogScrubberJob> logger,
IProfilingLogger profilingLogger)
{
_auditService = auditService;
_settings = settings.CurrentValue;
_scopeProvider = scopeProvider;
@@ -57,17 +55,14 @@ public class LogScrubberJob : IRecurringBackgroundJob
settings.OnChange(x => _settings = x);
}
public Task RunJobAsync()
public async Task RunJobAsync()
{
// Ensure we use an explicit scope since we are running on a background thread.
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
using (_profilingLogger.DebugDuration<LogScrubberJob>("Log scrubbing executing", "Log scrubbing complete"))
{
_auditService.CleanLogs((int)_settings.MaxLogAge.TotalMinutes);
await _auditService.CleanLogsAsync((int)_settings.MaxLogAge.TotalMinutes);
_ = scope.Complete();
}
return Task.CompletedTask;
}
}