< Summary

Information
Class: ArturRios.Logging.StandaloneLogger
Assembly: ArturRios.Logging
File(s): /home/ci/agent/workspace/dotnet-logging-docs_main/app/src/StandaloneLogger.cs
Line coverage
97%
Covered lines: 33
Uncovered lines: 1
Coverable lines: 34
Total lines: 167
Line coverage: 97%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage
100%
Covered methods: 11
Fully covered methods: 10
Total methods: 11
Method coverage: 100%
Full method coverage: 90.9%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%2283.33%
get_TraceId()100%11100%
Trace(...)100%22100%
Debug(...)100%22100%
Info(...)100%22100%
Warn(...)100%22100%
Error(...)100%22100%
Exception(...)100%22100%
Critical(...)100%22100%
Fatal(...)100%22100%
FormatMessageWithTraceId(...)100%22100%

File(s)

/home/ci/agent/workspace/dotnet-logging-docs_main/app/src/StandaloneLogger.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using ArturRios.Logging.Configuration;
 3using ArturRios.Logging.Factories;
 4using ArturRios.Logging.Interfaces;
 5
 6namespace ArturRios.Logging;
 7
 8/// <summary>
 9/// A standalone logger that automatically captures caller information using compiler attributes.
 10/// Supports multiple logger configurations and optional trace ID tracking.
 11/// </summary>
 12public class StandaloneLogger : IStandaloneLogger
 13{
 2014    private readonly List<IInternalLogger> _loggers = [];
 15
 16    /// <summary>
 17    /// Gets or sets the trace ID for correlating log entries across related operations.
 18    /// </summary>
 3519    public string? TraceId { get; set; }
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="StandaloneLogger"/> class with the specified configurations.
 23    /// </summary>
 24    /// <param name="configurations">The list of logger configurations to use.</param>
 25    /// <exception cref="ArgumentNullException"><paramref name="configurations"/>, or one of its items, is <c>null</c>.<
 2026    public StandaloneLogger(List<LoggerConfiguration> configurations)
 27    {
 2028        ArgumentNullException.ThrowIfNull(configurations);
 29
 3830        foreach (var config in configurations)
 31        {
 032            _loggers.Add(InternalLoggerFactory.Create(config));
 33        }
 1934    }
 35
 36    /// <summary>
 37    /// Logs a trace-level message with automatic caller information capture.
 38    /// </summary>
 39    /// <param name="message">The message to log.</param>
 40    /// <param name="filePath">The source file path (automatically captured).</param>
 41    /// <param name="methodName">The calling method name (automatically captured).</param>
 42    public void Trace(string message, [CallerFilePath] string filePath = "unknown",
 43        [CallerMemberName] string methodName = "unknown")
 44    {
 1645        foreach (var logger in _loggers)
 46        {
 447            logger.Trace(FormatMessageWithTraceId(message), filePath, methodName);
 48        }
 449    }
 50
 51    /// <summary>
 52    /// Logs a debug-level message with automatic caller information capture.
 53    /// </summary>
 54    /// <param name="message">The message to log.</param>
 55    /// <param name="filePath">The source file path (automatically captured).</param>
 56    /// <param name="methodName">The calling method name (automatically captured).</param>
 57    public void Debug(string message, [CallerFilePath] string filePath = "unknown",
 58        [CallerMemberName] string methodName = "unknown")
 59    {
 860        foreach (var logger in _loggers)
 61        {
 262            logger.Debug(FormatMessageWithTraceId(message), filePath, methodName);
 63        }
 264    }
 65
 66    /// <summary>
 67    /// Logs an information-level message with automatic caller information capture.
 68    /// </summary>
 69    /// <param name="message">The message to log.</param>
 70    /// <param name="filePath">The source file path (automatically captured).</param>
 71    /// <param name="methodName">The calling method name (automatically captured).</param>
 72    public void Info(string message, [CallerFilePath] string filePath = "unknown",
 73        [CallerMemberName] string methodName = "unknown")
 74    {
 1075        foreach (var logger in _loggers)
 76        {
 377            logger.Info(FormatMessageWithTraceId(message), filePath, methodName);
 78        }
 279    }
 80
 81    /// <summary>
 82    /// Logs a warning-level message with automatic caller information capture.
 83    /// </summary>
 84    /// <param name="message">The message to log.</param>
 85    /// <param name="filePath">The source file path (automatically captured).</param>
 86    /// <param name="methodName">The calling method name (automatically captured).</param>
 87    public void Warn(string message, [CallerFilePath] string filePath = "unknown",
 88        [CallerMemberName] string methodName = "unknown")
 89    {
 890        foreach (var logger in _loggers)
 91        {
 292            logger.Warn(FormatMessageWithTraceId(message), filePath, methodName);
 93        }
 294    }
 95
 96    /// <summary>
 97    /// Logs an error-level message with automatic caller information capture.
 98    /// </summary>
 99    /// <param name="message">The message to log.</param>
 100    /// <param name="filePath">The source file path (automatically captured).</param>
 101    /// <param name="methodName">The calling method name (automatically captured).</param>
 102    public void Error(string message, [CallerFilePath] string filePath = "unknown",
 103        [CallerMemberName] string methodName = "unknown")
 104    {
 8105        foreach (var logger in _loggers)
 106        {
 2107            logger.Error(FormatMessageWithTraceId(message), filePath, methodName);
 108        }
 2109    }
 110
 111    /// <summary>
 112    /// Logs an exception with automatic caller information capture.
 113    /// </summary>
 114    /// <remarks>
 115    /// The entry carries <see cref="System.Exception.ToString"/> — type, message, inner exceptions and stack trace —
 116    /// as <see cref="StateLogger.Exception"/> does.
 117    /// </remarks>
 118    /// <param name="exception">The exception to log.</param>
 119    /// <param name="filePath">The source file path (automatically captured).</param>
 120    /// <param name="methodName">The calling method name (automatically captured).</param>
 121    public void Exception(Exception exception, [CallerFilePath] string filePath = "unknown",
 122        [CallerMemberName] string methodName = "unknown")
 123    {
 3124        ArgumentNullException.ThrowIfNull(exception);
 125
 2126        var msg = FormatMessageWithTraceId(exception.ToString());
 8127        foreach (var logger in _loggers)
 128        {
 2129            logger.Exception(msg, filePath, methodName);
 130        }
 2131    }
 132
 133    /// <summary>
 134    /// Logs a critical-level message with automatic caller information capture.
 135    /// </summary>
 136    /// <param name="message">The message to log.</param>
 137    /// <param name="filePath">The source file path (automatically captured).</param>
 138    /// <param name="methodName">The calling method name (automatically captured).</param>
 139    public void Critical(string message, [CallerFilePath] string filePath = "unknown",
 140        [CallerMemberName] string methodName = "unknown")
 141    {
 8142        foreach (var logger in _loggers)
 143        {
 2144            logger.Critical(FormatMessageWithTraceId(message), filePath, methodName);
 145        }
 2146    }
 147
 148    /// <summary>
 149    /// Logs a fatal-level message with automatic caller information capture.
 150    /// </summary>
 151    /// <param name="message">The message to log.</param>
 152    /// <param name="filePath">The source file path (automatically captured).</param>
 153    /// <param name="methodName">The calling method name (automatically captured).</param>
 154    public void Fatal(string message, [CallerFilePath] string filePath = "unknown",
 155        [CallerMemberName] string methodName = "unknown")
 156    {
 8157        foreach (var logger in _loggers)
 158        {
 2159            logger.Fatal(FormatMessageWithTraceId(message), filePath, methodName);
 160        }
 2161    }
 162
 163    private string FormatMessageWithTraceId(string message)
 164    {
 19165        return !string.IsNullOrEmpty(TraceId) ? $"[{nameof(TraceId)}] {TraceId} | {message}" : message;
 166    }
 167}