| | | 1 | | using ArturRios.Logging.Configuration; |
| | | 2 | | using ArturRios.Logging.Factories; |
| | | 3 | | using ArturRios.Logging.Interfaces; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Logging; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A logger that extracts caller information from state objects passed with log messages. |
| | | 9 | | /// Useful for integration with dependency injection frameworks and structured logging. |
| | | 10 | | /// </summary> |
| | | 11 | | public class StateLogger : IStateLogger |
| | | 12 | | { |
| | 28 | 13 | | private readonly List<IInternalLogger> _loggers = []; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets the trace ID for correlating log entries across related operations. |
| | | 17 | | /// </summary> |
| | 41 | 18 | | public string? TraceId { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="StateLogger"/> class with the specified configurations. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="configurations">The list of logger configurations to use.</param> |
| | | 24 | | /// <exception cref="ArgumentNullException"><paramref name="configurations"/>, or one of its items, is <c>null</c>.< |
| | 28 | 25 | | public StateLogger(List<LoggerConfiguration> configurations) |
| | | 26 | | { |
| | 28 | 27 | | ArgumentNullException.ThrowIfNull(configurations); |
| | | 28 | | |
| | 54 | 29 | | foreach (var config in configurations) |
| | | 30 | | { |
| | 0 | 31 | | _loggers.Add(InternalLoggerFactory.Create(config)); |
| | | 32 | | } |
| | 27 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Logs a trace-level message with caller information extracted from state. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="message">The message to log.</param> |
| | | 39 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 40 | | public void Trace(string message, object? state = null) |
| | | 41 | | { |
| | 3 | 42 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 12 | 43 | | foreach (var logger in _loggers) |
| | | 44 | | { |
| | 3 | 45 | | logger.Trace(FormatMessageWithTraceId(message), fp, mn); |
| | | 46 | | } |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Logs a debug-level message with caller information extracted from state. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="message">The message to log.</param> |
| | | 53 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 54 | | public void Debug(string message, object? state = null) |
| | | 55 | | { |
| | 4 | 56 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 16 | 57 | | foreach (var logger in _loggers) |
| | | 58 | | { |
| | 4 | 59 | | logger.Debug(FormatMessageWithTraceId(message), fp, mn); |
| | | 60 | | } |
| | 4 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Logs an information-level message with caller information extracted from state. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="message">The message to log.</param> |
| | | 67 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 68 | | public void Info(string message, object? state = null) |
| | | 69 | | { |
| | 5 | 70 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 22 | 71 | | foreach (var logger in _loggers) |
| | | 72 | | { |
| | 6 | 73 | | logger.Info(FormatMessageWithTraceId(message), fp, mn); |
| | | 74 | | } |
| | 5 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Logs a warning-level message with caller information extracted from state. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="message">The message to log.</param> |
| | | 81 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 82 | | public void Warn(string message, object? state = null) |
| | | 83 | | { |
| | 3 | 84 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 12 | 85 | | foreach (var logger in _loggers) |
| | | 86 | | { |
| | 3 | 87 | | logger.Warn(FormatMessageWithTraceId(message), fp, mn); |
| | | 88 | | } |
| | 3 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Logs an error-level message with caller information extracted from state. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="message">The message to log.</param> |
| | | 95 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 96 | | public void Error(string message, object? state = null) |
| | | 97 | | { |
| | 3 | 98 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 12 | 99 | | foreach (var logger in _loggers) |
| | | 100 | | { |
| | 3 | 101 | | logger.Error(FormatMessageWithTraceId(message), fp, mn); |
| | | 102 | | } |
| | 3 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Logs an exception with caller information extracted from state. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="exception">The exception to log.</param> |
| | | 109 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 110 | | public void Exception(Exception exception, object? state = null) |
| | | 111 | | { |
| | 3 | 112 | | ArgumentNullException.ThrowIfNull(exception); |
| | | 113 | | |
| | 3 | 114 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 3 | 115 | | var msg = FormatMessageWithTraceId(exception.ToString()); |
| | 12 | 116 | | foreach (var logger in _loggers) |
| | | 117 | | { |
| | 3 | 118 | | logger.Exception(msg, fp, mn); |
| | | 119 | | } |
| | 3 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Logs a critical-level message with caller information extracted from state. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="message">The message to log.</param> |
| | | 126 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 127 | | public void Critical(string message, object? state = null) |
| | | 128 | | { |
| | 3 | 129 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 12 | 130 | | foreach (var logger in _loggers) |
| | | 131 | | { |
| | 3 | 132 | | logger.Critical(FormatMessageWithTraceId(message), fp, mn); |
| | | 133 | | } |
| | 3 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Logs a fatal-level message with caller information extracted from state. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="message">The message to log.</param> |
| | | 140 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 141 | | public void Fatal(string message, object? state = null) |
| | | 142 | | { |
| | 3 | 143 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 12 | 144 | | foreach (var logger in _loggers) |
| | | 145 | | { |
| | 3 | 146 | | logger.Fatal(FormatMessageWithTraceId(message), fp, mn); |
| | | 147 | | } |
| | 3 | 148 | | } |
| | | 149 | | |
| | | 150 | | private static (string filePath, string methodName) ResolveCallerInfo(object? state) |
| | | 151 | | { |
| | 27 | 152 | | string? filePath = null; |
| | 27 | 153 | | string? methodName = null; |
| | | 154 | | |
| | 27 | 155 | | if (state is not IEnumerable<KeyValuePair<string, object>> pairs) |
| | | 156 | | { |
| | 7 | 157 | | return (filePath ?? "unknown", methodName ?? "unknown"); |
| | | 158 | | } |
| | | 159 | | |
| | 101 | 160 | | foreach (var (key, value) in pairs) |
| | | 161 | | { |
| | 40 | 162 | | if (value is null) |
| | | 163 | | { |
| | | 164 | | continue; |
| | | 165 | | } |
| | | 166 | | |
| | 39 | 167 | | if (filePath == null && (string.Equals(key, "CallerFilePath", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 168 | | string.Equals(key, "FilePath", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 169 | | string.Equals(key, "callerFilePath", StringComparison.OrdinalIgnoreCase))) |
| | | 170 | | { |
| | 19 | 171 | | filePath = value.ToString(); |
| | | 172 | | } |
| | | 173 | | |
| | 39 | 174 | | if (methodName == null && (string.Equals(key, "CallerMemberName", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 175 | | string.Equals(key, "MemberName", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 176 | | string.Equals(key, "Method", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 177 | | string.Equals(key, "callerMemberName", StringComparison.OrdinalIgnoreCase))) |
| | | 178 | | { |
| | 20 | 179 | | methodName = value.ToString(); |
| | | 180 | | } |
| | | 181 | | |
| | 39 | 182 | | if (filePath != null && methodName != null) |
| | 19 | 183 | | break; |
| | | 184 | | } |
| | | 185 | | |
| | 20 | 186 | | return (filePath ?? "unknown", methodName ?? "unknown"); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | private string FormatMessageWithTraceId(string message) |
| | | 190 | | { |
| | 28 | 191 | | return !string.IsNullOrEmpty(TraceId) ? $"[{nameof(TraceId)}] {TraceId} | {message}" : message; |
| | | 192 | | } |
| | | 193 | | } |