< Summary

Information
Class: Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services.RelationalStructuredLogMapper
Assembly: Elsa.Diagnostics.StructuredLogs.Persistence.Relational
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs.Persistence.Relational/Services/RelationalStructuredLogMapper.cs
Line coverage
100%
Covered lines: 55
Uncovered lines: 0
Coverable lines: 55
Total lines: 95
Line coverage: 100%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Map(...)50%22100%
Map(...)100%11100%
FormatTimestamp(...)100%11100%
ParseTimestamp(...)100%11100%
ReadNullableString(...)100%22100%
DeserializeDictionary(...)75%44100%
Deserialize(...)100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs.Persistence.Relational/Services/RelationalStructuredLogMapper.cs

#LineLine coverage
 1using System.Data.Common;
 2using System.Globalization;
 3using System.Text.Json;
 4using Elsa.Diagnostics.StructuredLogs.Models;
 5using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Models;
 6
 7namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services;
 8
 9public class RelationalStructuredLogMapper
 10{
 111    private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
 12
 13    public RelationalStructuredLogRecord Map(StructuredLogEvent logEvent)
 14    {
 215        return new()
 216        {
 217            Id = logEvent.Id,
 218            Sequence = logEvent.Sequence,
 219            Timestamp = FormatTimestamp(logEvent.Timestamp),
 220            ReceivedAt = FormatTimestamp(logEvent.ReceivedAt),
 221            Level = logEvent.Level,
 222            Category = logEvent.Category,
 223            EventId = logEvent.EventId,
 224            EventName = logEvent.EventName,
 225            Message = logEvent.Message,
 226            MessageTemplate = logEvent.MessageTemplate,
 227            ExceptionJson = logEvent.Exception == null ? null : JsonSerializer.Serialize(logEvent.Exception, JsonOptions
 228            ScopesJson = JsonSerializer.Serialize(logEvent.Scopes, JsonOptions),
 229            PropertiesJson = JsonSerializer.Serialize(logEvent.Properties, JsonOptions),
 230            TraceId = logEvent.TraceId,
 231            SpanId = logEvent.SpanId,
 232            CorrelationId = logEvent.CorrelationId,
 233            TenantId = logEvent.TenantId,
 234            WorkflowDefinitionId = logEvent.WorkflowDefinitionId,
 235            WorkflowInstanceId = logEvent.WorkflowInstanceId,
 236            SourceId = logEvent.SourceId
 237        };
 38    }
 39
 40    public StructuredLogEvent Map(DbDataReader reader)
 41    {
 342        return new()
 343        {
 344            Id = reader.GetString(reader.GetOrdinal("Id")),
 345            Sequence = reader.GetInt64(reader.GetOrdinal("Sequence")),
 346            Timestamp = ParseTimestamp(reader.GetString(reader.GetOrdinal("Timestamp"))),
 347            ReceivedAt = ParseTimestamp(reader.GetString(reader.GetOrdinal("ReceivedAt"))),
 348            Level = (StructuredLogLevel)reader.GetInt32(reader.GetOrdinal("Level")),
 349            Category = reader.GetString(reader.GetOrdinal("Category")),
 350            EventId = reader.GetInt32(reader.GetOrdinal("EventId")),
 351            EventName = ReadNullableString(reader, "EventName"),
 352            Message = reader.GetString(reader.GetOrdinal("Message")),
 353            MessageTemplate = ReadNullableString(reader, "MessageTemplate"),
 354            Exception = Deserialize<StructuredLogException>(ReadNullableString(reader, "ExceptionJson")),
 355            Scopes = DeserializeDictionary(ReadNullableString(reader, "ScopesJson")),
 356            Properties = DeserializeDictionary(ReadNullableString(reader, "PropertiesJson")),
 357            TraceId = ReadNullableString(reader, "TraceId"),
 358            SpanId = ReadNullableString(reader, "SpanId"),
 359            CorrelationId = ReadNullableString(reader, "CorrelationId"),
 360            TenantId = ReadNullableString(reader, "TenantId"),
 361            WorkflowDefinitionId = ReadNullableString(reader, "WorkflowDefinitionId"),
 362            WorkflowInstanceId = ReadNullableString(reader, "WorkflowInstanceId"),
 363            SourceId = reader.GetString(reader.GetOrdinal("SourceId"))
 364        };
 65    }
 66
 67    public static string FormatTimestamp(DateTimeOffset timestamp)
 68    {
 1169        return timestamp.ToUniversalTime().ToString("O", CultureInfo.InvariantCulture);
 70    }
 71
 72    public static DateTimeOffset ParseTimestamp(string timestamp)
 73    {
 774        return DateTimeOffset.Parse(timestamp, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeSt
 75    }
 76
 77    private static string? ReadNullableString(DbDataReader reader, string name)
 78    {
 3379        var ordinal = reader.GetOrdinal(name);
 3380        return reader.IsDBNull(ordinal) ? null : reader.GetString(ordinal);
 81    }
 82
 83    private static IDictionary<string, string?> DeserializeDictionary(string? json)
 84    {
 685        if (string.IsNullOrWhiteSpace(json))
 486            return new Dictionary<string, string?>();
 87
 288        return JsonSerializer.Deserialize<Dictionary<string, string?>>(json, JsonOptions) ?? new Dictionary<string, stri
 89    }
 90
 91    private static T? Deserialize<T>(string? json)
 92    {
 393        return string.IsNullOrWhiteSpace(json) ? default : JsonSerializer.Deserialize<T>(json, JsonOptions);
 94    }
 95}