< Summary

Information
Class: ArturRios.Extensions.ExceptionExtensions
Assembly: ArturRios.Extensions
File(s): /home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/ExceptionExtensions.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 47
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage
100%
Covered methods: 2
Fully covered methods: 2
Total methods: 2
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToLogLine(...)100%22100%
EscapeLineBreaks(...)100%11100%

File(s)

/home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/ExceptionExtensions.cs

#LineLine coverage
 1using System.Globalization;
 2
 3namespace ArturRios.Extensions;
 4
 5/// <summary>
 6///     Provides extension methods for Exceptions, including utilities to format an exception into a log line.
 7/// </summary>
 8public static class ExceptionExtensions
 9{
 10    /// <summary>
 11    ///     Formats exception details into a single log line and outputs a generated trace identifier.
 12    /// </summary>
 13    /// <param name="exception">The exception to format.</param>
 14    /// <param name="traceId">The generated trace identifier associated with this log entry.</param>
 15    /// <returns>A single-line string containing timestamp, trace id, exception type, message and stack trace.</returns>
 16    /// <remarks>
 17    ///     Line breaks in the message and the stack trace are written as the two-character escape <c>\n</c>,
 18    ///     so the result really is one line. A stack trace spans one line per frame, and a message can carry
 19    ///     line breaks taken from untrusted input; written raw, either one split the entry across several
 20    ///     lines of a line-oriented log, and a crafted message could forge entries that look genuine.
 21    ///     The timestamp is UTC, written as <c>yyyy-MM-dd HH:mm:ss</c> with the invariant culture, so it reads
 22    ///     the same whatever the current culture's time separator or calendar.
 23    /// </remarks>
 24    public static string ToLogLine(this Exception exception, out Guid traceId)
 25    {
 3426        traceId = Guid.NewGuid();
 27
 3428        var message = EscapeLineBreaks(exception.Message);
 3229        var stackTrace = EscapeLineBreaks(exception.StackTrace ?? "No stack trace available");
 30
 3231        var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
 32
 3233        return
 3234            $"{timestamp} | TraceId: {traceId} | Exception: {exception.GetType().Name} | Message: {message} | StackTrace
 35    }
 36
 37    /// <summary>
 38    ///     Replaces every line break — CR LF, LF, CR, and the Unicode line and paragraph separators — with
 39    ///     the literal escape <c>\n</c>.
 40    /// </summary>
 41    private static string EscapeLineBreaks(string value) =>
 6442        value.Replace("\r\n", "\\n")
 6443            .Replace('\r', '\n')
 6444            .Replace('\u2028', '\n')
 6445            .Replace('\u2029', '\n')
 6446            .Replace("\n", "\\n");
 47}