< Summary

Information
Class: ArturRios.Logging.Adapter.LoggerCallerExtensions
Assembly: ArturRios.Logging
File(s): D:\Repositories\dotnet-logging\src\Adapter\LoggerCallerExtensions.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 105
Line coverage: 100%
Branch coverage
30%
Covered branches: 3
Total branches: 10
Branch coverage: 30%
Method coverage
100%
Covered methods: 8
Fully covered methods: 7
Total methods: 8
Method coverage: 100%
Full method coverage: 87.5%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LogTraceWithCaller(...)100%11100%
LogDebugWithCaller(...)100%11100%
LogInformationWithCaller(...)100%11100%
LogWarningWithCaller(...)100%11100%
LogErrorWithCaller(...)100%11100%
LogCriticalWithCaller(...)100%11100%
LogExceptionWithCaller(...)100%22100%
LogWithCaller(...)12.5%88100%

File(s)

D:\Repositories\dotnet-logging\src\Adapter\LoggerCallerExtensions.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Microsoft.Extensions.Logging;
 3
 4namespace ArturRios.Logging.Adapter;
 5
 6/// <summary>
 7/// Extension methods for Microsoft.Extensions.Logging.ILogger that capture caller information.
 8/// </summary>
 9public static class LoggerCallerExtensions
 10{
 11    /// <param name="logger">The logger instance.</param>
 12    extension(ILogger logger)
 13    {
 14        /// <summary>
 15        /// Logs a trace-level message with automatic caller information capture.
 16        /// </summary>
 17        /// <param name="message">The message to log.</param>
 18        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 19        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 20        public void LogTraceWithCaller(string message,
 21            [CallerFilePath] string callerFilePath = "unknown",
 22            [CallerMemberName] string callerMemberName = "unknown")
 423            => LogWithCaller(logger, LogLevel.Trace, message, null, callerFilePath, callerMemberName);
 24
 25        /// <summary>
 26        /// Logs a debug-level message with automatic caller information capture.
 27        /// </summary>
 28        /// <param name="message">The message to log.</param>
 29        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 30        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 31        public void LogDebugWithCaller(string message,
 32            [CallerFilePath] string callerFilePath = "unknown",
 33            [CallerMemberName] string callerMemberName = "unknown")
 334            => LogWithCaller(logger, LogLevel.Debug, message, null, callerFilePath, callerMemberName);
 35
 36        /// <summary>
 37        /// Logs an information-level message with automatic caller information capture.
 38        /// </summary>
 39        /// <param name="message">The message to log.</param>
 40        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 41        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 42        public void LogInformationWithCaller(string message,
 43            [CallerFilePath] string callerFilePath = "unknown",
 44            [CallerMemberName] string callerMemberName = "unknown")
 345            => LogWithCaller(logger, LogLevel.Information, message, null, callerFilePath, callerMemberName);
 46
 47        /// <summary>
 48        /// Logs a warning-level message with automatic caller information capture.
 49        /// </summary>
 50        /// <param name="message">The message to log.</param>
 51        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 52        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 53        public void LogWarningWithCaller(string message,
 54            [CallerFilePath] string callerFilePath = "unknown",
 55            [CallerMemberName] string callerMemberName = "unknown")
 356            => LogWithCaller(logger, LogLevel.Warning, message, null, callerFilePath, callerMemberName);
 57
 58        /// <summary>
 59        /// Logs an error-level message with automatic caller information capture.
 60        /// </summary>
 61        /// <param name="message">The message to log.</param>
 62        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 63        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 64        public void LogErrorWithCaller(string message,
 65            [CallerFilePath] string callerFilePath = "unknown",
 66            [CallerMemberName] string callerMemberName = "unknown")
 367            => LogWithCaller(logger, LogLevel.Error, message, null, callerFilePath, callerMemberName);
 68
 69        /// <summary>
 70        /// Logs a critical-level message with automatic caller information capture.
 71        /// </summary>
 72        /// <param name="message">The message to log.</param>
 73        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 74        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 75        public void LogCriticalWithCaller(string message,
 76            [CallerFilePath] string callerFilePath = "unknown",
 77            [CallerMemberName] string callerMemberName = "unknown")
 378            => LogWithCaller(logger, LogLevel.Critical, message, null, callerFilePath, callerMemberName);
 79
 80        /// <summary>
 81        /// Logs an exception with automatic caller information capture.
 82        /// </summary>
 83        /// <param name="exception">The exception to log.</param>
 84        /// <param name="message">Optional message to log (defaults to exception message).</param>
 85        /// <param name="callerFilePath">The source file path (automatically captured).</param>
 86        /// <param name="callerMemberName">The calling method name (automatically captured).</param>
 87        public void LogExceptionWithCaller(Exception exception, string? message = null,
 88            [CallerFilePath] string callerFilePath = "unknown",
 89            [CallerMemberName] string callerMemberName = "unknown")
 590            => LogWithCaller(logger, LogLevel.Error, message ?? exception.Message, exception, callerFilePath, callerMemb
 91    }
 92
 93    private static void LogWithCaller(ILogger logger, LogLevel level, string? message, Exception? exception,
 94        string callerFilePath, string callerMemberName)
 2495    {
 2496        var state = new[]
 2497        {
 2498            new KeyValuePair<string, object>("CallerFilePath", callerFilePath),
 2499            new KeyValuePair<string, object>("CallerMemberName", callerMemberName),
 24100            new KeyValuePair<string, object>("OriginalMessage", message ?? string.Empty)
 24101        };
 102
 24103        logger.Log(level, new EventId(), state, exception, (_, ex) => message ?? ex?.Message ?? string.Empty);
 24104    }
 105}