|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | + |
| 3 | +namespace mcp_nexus.Utilities; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Provides conditional logging that respects both compilation symbols and environment settings. |
| 7 | +/// Trace and Debug logging are only enabled in Development environment or Debug builds. |
| 8 | +/// </summary> |
| 9 | +public static class ConditionalLogger |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Logs a trace message only if trace logging is enabled. |
| 13 | + /// </summary> |
| 14 | + public static void LogTrace(ILogger logger, string message, params object[] args) |
| 15 | + { |
| 16 | +#if ENABLE_TRACE_LOGGING |
| 17 | + logger.LogTrace(message, args); |
| 18 | +#endif |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Logs a trace message with exception only if trace logging is enabled. |
| 23 | + /// </summary> |
| 24 | + public static void LogTrace(ILogger logger, Exception exception, string message, params object[] args) |
| 25 | + { |
| 26 | +#if ENABLE_TRACE_LOGGING |
| 27 | + logger.LogTrace(exception, message, args); |
| 28 | +#endif |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Logs a debug message only if debug logging is enabled. |
| 33 | + /// </summary> |
| 34 | + public static void LogDebug(ILogger logger, string message, params object[] args) |
| 35 | + { |
| 36 | +#if DEBUG |
| 37 | + logger.LogDebug(message, args); |
| 38 | +#endif |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Logs a debug message with exception only if debug logging is enabled. |
| 43 | + /// </summary> |
| 44 | + public static void LogDebug(ILogger logger, Exception exception, string message, params object[] args) |
| 45 | + { |
| 46 | +#if DEBUG |
| 47 | + logger.LogDebug(exception, message, args); |
| 48 | +#endif |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Checks if trace logging is enabled. |
| 53 | + /// </summary> |
| 54 | + public static bool IsTraceEnabled(ILogger logger) |
| 55 | + { |
| 56 | +#if ENABLE_TRACE_LOGGING |
| 57 | + return logger.IsEnabled(LogLevel.Trace); |
| 58 | +#else |
| 59 | + return false; |
| 60 | +#endif |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Checks if debug logging is enabled. |
| 65 | + /// </summary> |
| 66 | + public static bool IsDebugEnabled(ILogger logger) |
| 67 | + { |
| 68 | +#if DEBUG |
| 69 | + return logger.IsEnabled(LogLevel.Debug); |
| 70 | +#else |
| 71 | + return false; |
| 72 | +#endif |
| 73 | + } |
| 74 | +} |
0 commit comments