< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Logging.Sinks.PowerShellSink
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Logging/Sinks/PowerShellSink.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 63
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
.cctor()100%11100%
get_TextFormatter()100%11100%
get_Callback()100%11100%
Emit(...)100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Logging/Sinks/PowerShellSink.cs

#LineLine coverage
 1using Serilog.Core;
 2using Serilog.Events;
 3using Serilog.Formatting;
 4using Serilog.Formatting.Display;
 5
 6namespace Kestrun.Logging.Sinks;
 7
 8/// <summary>
 9/// A Serilog sink that formats log events and invokes a callback for PowerShell integration.
 10/// </summary>
 11/// <remarks>
 12/// Initializes a new instance of the <see cref="PowerShellSink"/> class.
 13/// </remarks>
 14/// <param name="callback">The callback action invoked with the log event and its formatted message.</param>
 15/// <param name="outputTemplate">The output template used for formatting log messages.</param>
 16/// <exception cref="ArgumentNullException">Thrown if <paramref name="callback"/> is null.</exception>
 17/// <remarks>
 18/// This constructor initializes the text formatter and callback action for the sink.
 19/// </remarks>
 820public class PowerShellSink(Action<LogEvent, string> callback, string outputTemplate = PowerShellSink.DEFAULT_OUTPUT_TEM
 21{
 22    /// <summary>
 23    /// The default output template used for formatting log messages.
 24    /// </summary>
 25    public const string DEFAULT_OUTPUT_TEMPLATE = "{Message:lj}";
 26
 27#if NET9_0_OR_GREATER
 128    private static readonly Lock _syncRoot = new();
 29#else
 30    private static readonly object _syncRoot = new();
 31#endif
 32    /// <summary>
 33    /// Gets or sets the text formatter used to format log events.
 34    /// </summary>
 1735    public ITextFormatter TextFormatter { get; set; } = new MessageTemplateTextFormatter(string.IsNullOrWhiteSpace(outpu
 36
 37    /// <summary>
 38    /// Gets or sets the callback action that is invoked with the log event and its formatted message.
 39    /// </summary>
 1740    public Action<LogEvent, string> Callback { get; set; } = callback ?? throw new ArgumentNullException(nameof(callback
 41
 42    /// <summary>
 43    /// Emits a log event by formatting it and invoking the callback action.
 44    /// </summary>
 45    /// <param name="logEvent">The log event to emit.</param>
 46    /// <exception cref="ArgumentNullException">Thrown if <paramref name="logEvent"/> is null.</exception>
 47    /// <remarks>
 48    /// This method formats the log event using the specified text formatter and invokes the callback with the formatted
 49    /// </remarks>
 50    public void Emit(LogEvent logEvent)
 51    {
 852        ArgumentNullException.ThrowIfNull(logEvent);
 53
 854        using StringWriter strWriter = new();
 855        TextFormatter.Format(logEvent, strWriter);
 856        var renderedMessage = strWriter.ToString();
 57
 58        lock (_syncRoot)
 59        {
 860            Callback(logEvent, renderedMessage);
 861        }
 862    }
 63}