< Summary

Information
Class: Services.Authorization.Services.AuditService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/Authorization/Services/AuditService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 53
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
LogAsync()0%342180%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/Authorization/Services/AuditService.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2using Microsoft.Extensions.Logging;
 3using OzonDomains.Models.Authorization;
 4using OzonRepositories.Context;
 5
 6namespace Services.Authorization.Services;
 7
 8public sealed class AuditService : IAuditService
 9{
 10    private readonly ApplicationDbContext _db;
 11    private readonly IHttpContextAccessor _http;
 12    private readonly ILogger<AuditService> _logger;
 13
 014    public AuditService(ApplicationDbContext db, IHttpContextAccessor http, ILogger<AuditService> logger)
 15    {
 016        _db = db;
 017        _http = http;
 018        _logger = logger;
 019    }
 20
 21    public async Task LogAsync(
 22        string action,
 23        string entityType,
 24        string? entityId = null,
 25        string? oldValue = null,
 26        string? newValue = null,
 27        string? comment = null,
 28        CancellationToken ct = default)
 29    {
 30        try
 31        {
 032            var ctx = _http.HttpContext;
 033            var entry = new AuditLogEntry
 034            {
 035                Action = action,
 036                EntityType = entityType,
 037                EntityId = entityId,
 038                OldValue = oldValue,
 039                NewValue = newValue,
 040                Comment = comment,
 041                ActorUserId = ctx?.User?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value,
 042                ActorUserName = ctx?.User?.Identity?.Name,
 043                IpAddress = ctx?.Connection?.RemoteIpAddress?.ToString()
 044            };
 045            _db.AuditLogEntries.Add(entry);
 046            await _db.SaveChangesAsync(ct);
 047        }
 048        catch (Exception ex)
 49        {
 050            _logger.LogError(ex, "Не удалось записать аудит {Action} {EntityType} {EntityId}", action, entityType, entit
 051        }
 052    }
 53}