< Summary

Information
Class: Services.SystemMonitoringServices.IsolatedSystemMonitoringSignalWriter
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/SystemMonitoringServices/IsolatedSystemMonitoringSignalWriter.cs
Line coverage
82%
Covered lines: 14
Uncovered lines: 3
Coverable lines: 17
Total lines: 36
Line coverage: 82.3%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RecordAsync()66.66%6678.57%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/SystemMonitoringServices/IsolatedSystemMonitoringSignalWriter.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using OzonDomains.Models;
 4using OzonRepositories.Context;
 5
 6namespace Services.SystemMonitoringServices;
 7
 8/// <summary>
 9/// Писатель пассивных сигналов со своим короткоживущим DbContext. Нужен singleton-сервисам и параллельному коду
 10/// (например, <c>OneCNomenclatureService</c>): общий scoped-контекст там небезопасен, а его SaveChanges
 11/// сохранил бы чужие незавершённые изменения. Сбой записи сигнала никогда не ломает основную операцию.
 12/// </summary>
 213public sealed class IsolatedSystemMonitoringSignalWriter(
 214    IDbContextFactory<ApplicationDbContext> dbFactory,
 215    ILogger<IsolatedSystemMonitoringSignalWriter> logger) : ISystemMonitoringSignalWriter
 16{
 17    public async Task RecordAsync(string sourceKey, string outcome, string? scopeKey = null, string? message = null, Can
 18    {
 19        try
 20        {
 121            await using var context = await dbFactory.CreateDbContextAsync(cancellationToken);
 122            context.SystemMonitoringSignals.Add(new SystemMonitoringSignal
 123            {
 124                SourceKey = sourceKey[..Math.Min(sourceKey.Length, 100)],
 125                Outcome = outcome[..Math.Min(outcome.Length, 16)],
 126                ScopeKey = scopeKey?[..Math.Min(scopeKey.Length, 120)],
 127                Message = message?[..Math.Min(message.Length, 500)]
 128            });
 129            await context.SaveChangesAsync(cancellationToken);
 130        }
 031        catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
 32        {
 033            logger.LogWarning(ex, "Не удалось записать пассивный сигнал {SourceKey}", sourceKey);
 034        }
 135    }
 36}