| | | 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 | | { |
| | 18 | 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> |
| | 18 | 25 | | public StandaloneLogger(List<LoggerConfiguration> configurations) |
| | 18 | 26 | | { |
| | 54 | 27 | | foreach (var config in configurations) |
| | 0 | 28 | | { |
| | 0 | 29 | | _loggers.Add(InternalLoggerFactory.Create(config)); |
| | 0 | 30 | | } |
| | 18 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Logs a trace-level message with automatic caller information capture. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="message">The message to log.</param> |
| | | 37 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 38 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 39 | | public void Trace(string message, [CallerFilePath] string filePath = "unknown", |
| | | 40 | | [CallerMemberName] string methodName = "unknown") |
| | 4 | 41 | | { |
| | 20 | 42 | | foreach (var logger in _loggers) |
| | 4 | 43 | | { |
| | 4 | 44 | | logger.Trace(FormatMessageWithTraceId(message), filePath, methodName); |
| | 4 | 45 | | } |
| | 4 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Logs a debug-level message with automatic caller information capture. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="message">The message to log.</param> |
| | | 52 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 53 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 54 | | public void Debug(string message, [CallerFilePath] string filePath = "unknown", |
| | | 55 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 56 | | { |
| | 10 | 57 | | foreach (var logger in _loggers) |
| | 2 | 58 | | { |
| | 2 | 59 | | logger.Debug(FormatMessageWithTraceId(message), filePath, methodName); |
| | 2 | 60 | | } |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Logs an information-level message with automatic caller information capture. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="message">The message to log.</param> |
| | | 67 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 68 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 69 | | public void Info(string message, [CallerFilePath] string filePath = "unknown", |
| | | 70 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 71 | | { |
| | 12 | 72 | | foreach (var logger in _loggers) |
| | 3 | 73 | | { |
| | 3 | 74 | | logger.Info(FormatMessageWithTraceId(message), filePath, methodName); |
| | 3 | 75 | | } |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Logs a warning-level message with automatic caller information capture. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="message">The message to log.</param> |
| | | 82 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 83 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 84 | | public void Warn(string message, [CallerFilePath] string filePath = "unknown", |
| | | 85 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 86 | | { |
| | 10 | 87 | | foreach (var logger in _loggers) |
| | 2 | 88 | | { |
| | 2 | 89 | | logger.Warn(FormatMessageWithTraceId(message), filePath, methodName); |
| | 2 | 90 | | } |
| | 2 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Logs an error-level message with automatic caller information capture. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="message">The message to log.</param> |
| | | 97 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 98 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 99 | | public void Error(string message, [CallerFilePath] string filePath = "unknown", |
| | | 100 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 101 | | { |
| | 10 | 102 | | foreach (var logger in _loggers) |
| | 2 | 103 | | { |
| | 2 | 104 | | logger.Error(FormatMessageWithTraceId(message), filePath, methodName); |
| | 2 | 105 | | } |
| | 2 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Logs an exception with automatic caller information capture. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="exception">The exception to log.</param> |
| | | 112 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 113 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 114 | | public void Exception(Exception exception, [CallerFilePath] string filePath = "unknown", |
| | | 115 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 116 | | { |
| | 10 | 117 | | foreach (var logger in _loggers) |
| | 2 | 118 | | { |
| | 2 | 119 | | logger.Exception(FormatMessageWithTraceId(exception.Message), filePath, methodName); |
| | 2 | 120 | | } |
| | 2 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Logs a critical-level message with automatic caller information capture. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="message">The message to log.</param> |
| | | 127 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 128 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 129 | | public void Critical(string message, [CallerFilePath] string filePath = "unknown", |
| | | 130 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 131 | | { |
| | 10 | 132 | | foreach (var logger in _loggers) |
| | 2 | 133 | | { |
| | 2 | 134 | | logger.Critical(FormatMessageWithTraceId(message), filePath, methodName); |
| | 2 | 135 | | } |
| | 2 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Logs a fatal-level message with automatic caller information capture. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="message">The message to log.</param> |
| | | 142 | | /// <param name="filePath">The source file path (automatically captured).</param> |
| | | 143 | | /// <param name="methodName">The calling method name (automatically captured).</param> |
| | | 144 | | public void Fatal(string message, [CallerFilePath] string filePath = "unknown", |
| | | 145 | | [CallerMemberName] string methodName = "unknown") |
| | 2 | 146 | | { |
| | 10 | 147 | | foreach (var logger in _loggers) |
| | 2 | 148 | | { |
| | 2 | 149 | | logger.Fatal(FormatMessageWithTraceId(message), filePath, methodName); |
| | 2 | 150 | | } |
| | 2 | 151 | | } |
| | | 152 | | |
| | | 153 | | private string FormatMessageWithTraceId(string message) |
| | 19 | 154 | | { |
| | 19 | 155 | | return !string.IsNullOrEmpty(TraceId) ? $"[{nameof(TraceId)}] {TraceId} | {message}" : message; |
| | 19 | 156 | | } |
| | | 157 | | } |