| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Logging.Adapter; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods for Microsoft.Extensions.Logging.ILogger that capture caller information. |
| | | 8 | | /// </summary> |
| | | 9 | | public 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") |
| | 4 | 23 | | => 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") |
| | 3 | 34 | | => 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") |
| | 3 | 45 | | => 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") |
| | 3 | 56 | | => 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") |
| | 3 | 67 | | => 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") |
| | 3 | 78 | | => 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") |
| | 5 | 90 | | => 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) |
| | 24 | 95 | | { |
| | 24 | 96 | | var state = new[] |
| | 24 | 97 | | { |
| | 24 | 98 | | new KeyValuePair<string, object>("CallerFilePath", callerFilePath), |
| | 24 | 99 | | new KeyValuePair<string, object>("CallerMemberName", callerMemberName), |
| | 24 | 100 | | new KeyValuePair<string, object>("OriginalMessage", message ?? string.Empty) |
| | 24 | 101 | | }; |
| | | 102 | | |
| | 24 | 103 | | logger.Log(level, new EventId(), state, exception, (_, ex) => message ?? ex?.Message ?? string.Empty); |
| | 24 | 104 | | } |
| | | 105 | | } |