| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using ArturRios.Logging.Configuration; |
| | | 3 | | using ArturRios.Logging.Factories; |
| | | 4 | | using ArturRios.Logging.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace 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> |
| | | 12 | | public class StandaloneLogger : IStandaloneLogger |
| | | 13 | | { |
| | 20 | 14 | | private readonly List<IInternalLogger> _loggers = []; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets or sets the trace ID for correlating log entries across related operations. |
| | | 18 | | /// </summary> |
| | 35 | 19 | | 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>.< |
| | 20 | 26 | | public StandaloneLogger(List<LoggerConfiguration> configurations) |
| | | 27 | | { |
| | 20 | 28 | | ArgumentNullException.ThrowIfNull(configurations); |
| | | 29 | | |
| | 38 | 30 | | foreach (var config in configurations) |
| | | 31 | | { |
| | 0 | 32 | | _loggers.Add(InternalLoggerFactory.Create(config)); |
| | | 33 | | } |
| | 19 | 34 | | } |
| | | 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 | | { |
| | 16 | 45 | | foreach (var logger in _loggers) |
| | | 46 | | { |
| | 4 | 47 | | logger.Trace(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 48 | | } |
| | 4 | 49 | | } |
| | | 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 | | { |
| | 8 | 60 | | foreach (var logger in _loggers) |
| | | 61 | | { |
| | 2 | 62 | | logger.Debug(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 63 | | } |
| | 2 | 64 | | } |
| | | 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 | | { |
| | 10 | 75 | | foreach (var logger in _loggers) |
| | | 76 | | { |
| | 3 | 77 | | logger.Info(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 78 | | } |
| | 2 | 79 | | } |
| | | 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 | | { |
| | 8 | 90 | | foreach (var logger in _loggers) |
| | | 91 | | { |
| | 2 | 92 | | logger.Warn(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 93 | | } |
| | 2 | 94 | | } |
| | | 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 | | { |
| | 8 | 105 | | foreach (var logger in _loggers) |
| | | 106 | | { |
| | 2 | 107 | | logger.Error(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 108 | | } |
| | 2 | 109 | | } |
| | | 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 | | { |
| | 3 | 124 | | ArgumentNullException.ThrowIfNull(exception); |
| | | 125 | | |
| | 2 | 126 | | var msg = FormatMessageWithTraceId(exception.ToString()); |
| | 8 | 127 | | foreach (var logger in _loggers) |
| | | 128 | | { |
| | 2 | 129 | | logger.Exception(msg, filePath, methodName); |
| | | 130 | | } |
| | 2 | 131 | | } |
| | | 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 | | { |
| | 8 | 142 | | foreach (var logger in _loggers) |
| | | 143 | | { |
| | 2 | 144 | | logger.Critical(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 145 | | } |
| | 2 | 146 | | } |
| | | 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 | | { |
| | 8 | 157 | | foreach (var logger in _loggers) |
| | | 158 | | { |
| | 2 | 159 | | logger.Fatal(FormatMessageWithTraceId(message), filePath, methodName); |
| | | 160 | | } |
| | 2 | 161 | | } |
| | | 162 | | |
| | | 163 | | private string FormatMessageWithTraceId(string message) |
| | | 164 | | { |
| | 19 | 165 | | return !string.IsNullOrEmpty(TraceId) ? $"[{nameof(TraceId)}] {TraceId} | {message}" : message; |
| | | 166 | | } |
| | | 167 | | } |