|
| 1 | +using System; |
| 2 | +using Cuemon; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | + |
| 5 | +namespace Codebelt.Bootstrapper |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Provides centralized logging messages for the Bootstrapper SDK using the LoggerMessage pattern. |
| 9 | + /// This API supports the product infrastructure and is not intended to be used directly from your code. |
| 10 | + /// </summary> |
| 11 | + /// <remarks>https://learn.microsoft.com/en-us/dotnet/core/extensions/high-performance-logging</remarks> |
| 12 | + public static class BootstrapperLogMessages |
| 13 | + { |
| 14 | + // Information messages |
| 15 | + private static readonly Action<ILogger, Exception> RunAsyncStartedDefinition = LoggerMessage.Define(LogLevel.Information, new EventId(1000, nameof(RunAsyncStarted)), "RunAsync started."); |
| 16 | + private static readonly Action<ILogger, Exception> RunAsyncPrematureEndDefinition = LoggerMessage.Define(LogLevel.Information, new EventId(1001, nameof(RunAsyncPrematureEnd)), "RunAsync ended prematurely."); |
| 17 | + private static readonly Action<ILogger, Exception> RunAsyncCompletedDefinition = LoggerMessage.Define(LogLevel.Information, new EventId(1002, nameof(RunAsyncCompleted)), "RunAsync completed successfully."); |
| 18 | + |
| 19 | + // Warning messages |
| 20 | + private static readonly Action<ILogger, string, Exception> UnableToActivateInstanceDefinition = LoggerMessage.Define<string>(LogLevel.Warning, new EventId(2000, nameof(UnableToActivateInstance)), "Unable to activate an instance of {TypeFullName}."); |
| 21 | + |
| 22 | + // Critical messages |
| 23 | + private static readonly Action<ILogger, string, Exception> FatalErrorActivatingDefinition = LoggerMessage.Define<string>(LogLevel.Critical, new EventId(3000, nameof(FatalErrorActivating)), "Fatal error occurred while activating {TypeFullName}."); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Logs that RunAsync has started. |
| 27 | + /// </summary> |
| 28 | + /// <param name="logger">The logger instance.</param> |
| 29 | + public static void RunAsyncStarted(this IDecorator<ILogger> logger) |
| 30 | + { |
| 31 | + RunAsyncStartedDefinition(logger.Inner, null); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Logs that RunAsync ended prematurely. |
| 36 | + /// </summary> |
| 37 | + /// <param name="logger">The logger instance.</param> |
| 38 | + public static void RunAsyncPrematureEnd(this IDecorator<ILogger> logger) |
| 39 | + { |
| 40 | + RunAsyncPrematureEndDefinition(logger.Inner, null); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Logs that RunAsync completed successfully. |
| 45 | + /// </summary> |
| 46 | + /// <param name="logger">The logger instance.</param> |
| 47 | + public static void RunAsyncCompleted(this IDecorator<ILogger> logger) |
| 48 | + { |
| 49 | + RunAsyncCompletedDefinition(logger.Inner, null); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Logs an inability to activate the specified type. |
| 54 | + /// </summary> |
| 55 | + /// <param name="logger">The logger instance.</param> |
| 56 | + /// <param name="typeFullName">The full name of the type that couldn't be activated.</param> |
| 57 | + public static void UnableToActivateInstance(this IDecorator<ILogger> logger, string typeFullName) |
| 58 | + { |
| 59 | + UnableToActivateInstanceDefinition(logger.Inner, typeFullName, null); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Logs a fatal error that occurred while activating a type. |
| 64 | + /// </summary> |
| 65 | + /// <param name="logger">The logger instance.</param> |
| 66 | + /// <param name="typeFullName">The full name of the type being activated.</param> |
| 67 | + /// <param name="exception">The exception that occurred.</param> |
| 68 | + public static void FatalErrorActivating(this IDecorator<ILogger> logger, string typeFullName, Exception exception) |
| 69 | + { |
| 70 | + FatalErrorActivatingDefinition(logger.Inner, typeFullName, exception); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments