| | | 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 | | { |
| | 27 | 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> |
| | 27 | 24 | | public StateLogger(List<LoggerConfiguration> configurations) |
| | 27 | 25 | | { |
| | 81 | 26 | | foreach (var config in configurations) |
| | 0 | 27 | | { |
| | 0 | 28 | | _loggers.Add(InternalLoggerFactory.Create(config)); |
| | 0 | 29 | | } |
| | 27 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Logs a trace-level message with caller information extracted from state. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="message">The message to log.</param> |
| | | 36 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 37 | | public void Trace(string message, object? state = null) |
| | 3 | 38 | | { |
| | 3 | 39 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 15 | 40 | | foreach (var logger in _loggers) |
| | 3 | 41 | | { |
| | 3 | 42 | | logger.Trace(FormatMessageWithTraceId(message), fp, mn); |
| | 3 | 43 | | } |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Logs a debug-level message with caller information extracted from state. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="message">The message to log.</param> |
| | | 50 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 51 | | public void Debug(string message, object? state = null) |
| | 4 | 52 | | { |
| | 4 | 53 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 20 | 54 | | foreach (var logger in _loggers) |
| | 4 | 55 | | { |
| | 4 | 56 | | logger.Debug(FormatMessageWithTraceId(message), fp, mn); |
| | 4 | 57 | | } |
| | 4 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Logs an information-level message with caller information extracted from state. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="message">The message to log.</param> |
| | | 64 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 65 | | public void Info(string message, object? state = null) |
| | 5 | 66 | | { |
| | 5 | 67 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 27 | 68 | | foreach (var logger in _loggers) |
| | 6 | 69 | | { |
| | 6 | 70 | | logger.Info(FormatMessageWithTraceId(message), fp, mn); |
| | 6 | 71 | | } |
| | 5 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Logs a warning-level message with caller information extracted from state. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="message">The message to log.</param> |
| | | 78 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 79 | | public void Warn(string message, object? state = null) |
| | 3 | 80 | | { |
| | 3 | 81 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 15 | 82 | | foreach (var logger in _loggers) |
| | 3 | 83 | | { |
| | 3 | 84 | | logger.Warn(FormatMessageWithTraceId(message), fp, mn); |
| | 3 | 85 | | } |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Logs an error-level message with caller information extracted from state. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="message">The message to log.</param> |
| | | 92 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 93 | | public void Error(string message, object? state = null) |
| | 3 | 94 | | { |
| | 3 | 95 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 15 | 96 | | foreach (var logger in _loggers) |
| | 3 | 97 | | { |
| | 3 | 98 | | logger.Error(FormatMessageWithTraceId(message), fp, mn); |
| | 3 | 99 | | } |
| | 3 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Logs an exception with caller information extracted from state. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="exception">The exception to log.</param> |
| | | 106 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 107 | | public void Exception(Exception exception, object? state = null) |
| | 3 | 108 | | { |
| | 3 | 109 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 3 | 110 | | var msg = FormatMessageWithTraceId(exception.ToString() ?? exception.Message); |
| | 15 | 111 | | foreach (var logger in _loggers) |
| | 3 | 112 | | { |
| | 3 | 113 | | logger.Exception(msg, fp, mn); |
| | 3 | 114 | | } |
| | 3 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Logs a critical-level message with caller information extracted from state. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="message">The message to log.</param> |
| | | 121 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 122 | | public void Critical(string message, object? state = null) |
| | 3 | 123 | | { |
| | 3 | 124 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 15 | 125 | | foreach (var logger in _loggers) |
| | 3 | 126 | | { |
| | 3 | 127 | | logger.Critical(FormatMessageWithTraceId(message), fp, mn); |
| | 3 | 128 | | } |
| | 3 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Logs a fatal-level message with caller information extracted from state. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="message">The message to log.</param> |
| | | 135 | | /// <param name="state">Optional state object containing caller information.</param> |
| | | 136 | | public void Fatal(string message, object? state = null) |
| | 3 | 137 | | { |
| | 3 | 138 | | var (fp, mn) = ResolveCallerInfo(state); |
| | 15 | 139 | | foreach (var logger in _loggers) |
| | 3 | 140 | | { |
| | 3 | 141 | | logger.Fatal(FormatMessageWithTraceId(message), fp, mn); |
| | 3 | 142 | | } |
| | 3 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static (string filePath, string methodName) ResolveCallerInfo(object? state) |
| | 27 | 146 | | { |
| | 27 | 147 | | string? filePath = null; |
| | 27 | 148 | | string? methodName = null; |
| | | 149 | | |
| | 27 | 150 | | if (state is not IEnumerable<KeyValuePair<string, object>> pairs) |
| | 7 | 151 | | { |
| | 7 | 152 | | return (filePath ?? "unknown", methodName ?? "unknown"); |
| | | 153 | | } |
| | | 154 | | |
| | 121 | 155 | | foreach (var (key, value) in pairs) |
| | 40 | 156 | | { |
| | 40 | 157 | | if (value is null) |
| | 1 | 158 | | { |
| | 1 | 159 | | continue; |
| | | 160 | | } |
| | | 161 | | |
| | 39 | 162 | | if (filePath == null && (string.Equals(key, "CallerFilePath", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 163 | | string.Equals(key, "FilePath", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 164 | | string.Equals(key, "callerFilePath", StringComparison.OrdinalIgnoreCase))) |
| | 19 | 165 | | { |
| | 19 | 166 | | filePath = value.ToString(); |
| | 19 | 167 | | } |
| | | 168 | | |
| | 39 | 169 | | if (methodName == null && (string.Equals(key, "CallerMemberName", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 170 | | string.Equals(key, "MemberName", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 171 | | string.Equals(key, "Method", StringComparison.OrdinalIgnoreCase) || |
| | 39 | 172 | | string.Equals(key, "callerMemberName", StringComparison.OrdinalIgnoreCase))) |
| | 20 | 173 | | { |
| | 20 | 174 | | methodName = value.ToString(); |
| | 20 | 175 | | } |
| | | 176 | | |
| | 58 | 177 | | if (filePath != null && methodName != null) break; |
| | 20 | 178 | | } |
| | | 179 | | |
| | 20 | 180 | | return (filePath ?? "unknown", methodName ?? "unknown"); |
| | 27 | 181 | | } |
| | | 182 | | |
| | | 183 | | private string FormatMessageWithTraceId(string message) |
| | 28 | 184 | | { |
| | 28 | 185 | | return !string.IsNullOrEmpty(TraceId) ? $"[{nameof(TraceId)}] {TraceId} | {message}" : message; |
| | 28 | 186 | | } |
| | | 187 | | } |