using JsonRPC4.Common; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace JsonRPC4.Router.Utilities { public static class LoggerExtensions { private static readonly Action attemptingToMatchMethod; private static readonly Action requestMatchedMethod; private static readonly Action methodsInRoute; private const LogLevel methodsInRouteLevel = LogLevel.Trace; private static readonly Action invokingBatchRequests; private static readonly Action batchRequestsComplete; private static readonly Action invokingRequest; private static readonly Action invokeMethod; private static readonly Action invokeMethodComplete; private static readonly Action finishedRequest; private static readonly Action finishedRequestNoId; private static readonly Action skippingAuth; private static readonly Action runningAuth; private static readonly Action authSuccessful; private static readonly Action authFailed; private static readonly Action noConfiguredAuth; private static readonly Action parsingRequests; private static readonly Action parsedRequests; private static readonly Action processingRequests; private static readonly Action responseFailedWithNoId; private static readonly Action noResponses; private static readonly Action responses; static LoggerExtensions() { attemptingToMatchMethod = LoggerMessage.Define(LogLevel.Debug, new EventId(1, "AttemptingToMatchMethod"), "Attempting to match Rpc request to a method '{Method}'"); requestMatchedMethod = LoggerMessage.Define(LogLevel.Debug, new EventId(2, "RequestMatchedMethod"), "Request was matched to a method"); methodsInRoute = LoggerMessage.Define(LogLevel.Trace, new EventId(3, "MethodsInRoute"), "Methods in route: {MethodsString}"); invokingBatchRequests = LoggerMessage.Define(LogLevel.Debug, new EventId(4, "InvokingBatchRequests"), "Invoking '{Count}' batch requests"); batchRequestsComplete = LoggerMessage.Define(LogLevel.Debug, new EventId(5, "BatchRequestsComplete"), "Finished batch requests"); invokingRequest = LoggerMessage.Define(LogLevel.Debug, new EventId(6, "InvokingRequest"), "Invoking request with id '{Id}'"); invokeMethod = LoggerMessage.Define(LogLevel.Debug, new EventId(7, "InvokeMethod"), "Attempting to invoke method '{Method}'"); invokeMethodComplete = LoggerMessage.Define(LogLevel.Debug, new EventId(8, "InvokeMethodComplete"), "Finished invoking method '{Method}'"); finishedRequest = LoggerMessage.Define(LogLevel.Debug, new EventId(9, "FinishedRequest"), "Finished request with id: {Id}"); finishedRequestNoId = LoggerMessage.Define(LogLevel.Debug, new EventId(10, "FinishedRequestNoId"), "Finished request with no id. Not returning a response"); skippingAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(11, "SkippingAuth"), "Skipping authorization. Allow anonymous specified for method."); runningAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(12, "RunningAuth"), "Running authorization for method."); authSuccessful = LoggerMessage.Define(LogLevel.Debug, new EventId(13, "AuthSuccessful"), "Authorization was successful."); authFailed = LoggerMessage.Define(LogLevel.Information, new EventId(14, "AuthFailed"), "Authorization failed."); noConfiguredAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(15, "NoConfiguredAuth"), "Skipping authorization. None configured for class or method."); parsingRequests = LoggerMessage.Define(LogLevel.Debug, new EventId(16, "ParsingRequests"), "Attempting to parse Rpc request from the json"); parsedRequests = LoggerMessage.Define(LogLevel.Debug, new EventId(17, "ParsedRequests"), "Successfully parsed {Count} Rpc request(s)"); processingRequests = LoggerMessage.Define(LogLevel.Information, new EventId(18, "ProcessingRequests"), "Processing {Count} Rpc requests"); responseFailedWithNoId = LoggerMessage.Define(LogLevel.Error, new EventId(19, "ResponseFailedWithNoId"), "Request with no id failed and no response will be sent. Error - Code: {Code}, Message: {Message}"); noResponses = LoggerMessage.Define(LogLevel.Information, new EventId(20, "NoResponses"), "No rpc responses created."); responses = LoggerMessage.Define(LogLevel.Information, new EventId(21, "Responses"), "{Count} rpc response(s) created."); } public static void LogException(this ILogger logger, Exception ex, string message = null) { message = ((message == null) ? $"{ex}" : $"{message}{Environment.NewLine}{ex}"); logger.LogError(default(EventId), ex, message); } public static void AttemptingToMatchMethod(this ILogger logger, string method) { attemptingToMatchMethod(logger, method, null); } public static void RequestMatchedMethod(this ILogger logger) { requestMatchedMethod(logger, null); } public static void MethodsInRoute(this ILogger logger, IEnumerable methods) { if (logger.IsEnabled(LogLevel.Trace)) { string arg = string.Join(", ", methods.Select((MethodInfo m) => m.Name)); methodsInRoute(logger, arg, null); } } public static void InvokingBatchRequests(this ILogger logger, int count) { invokingBatchRequests(logger, count, null); } public static void BatchRequestsComplete(this ILogger logger) { batchRequestsComplete(logger, null); } public static void InvokingRequest(this ILogger logger, RpcId id) { invokingRequest(logger, id, null); } public static void InvokeMethod(this ILogger logger, string method) { invokeMethod(logger, method, null); } public static void InvokeMethodComplete(this ILogger logger, string method) { invokeMethodComplete(logger, method, null); } public static void FinishedRequest(this ILogger logger, RpcId id) { finishedRequest(logger, id, null); } public static void FinishedRequestNoId(this ILogger logger) { finishedRequestNoId(logger, null); } public static void SkippingAuth(this ILogger logger) { skippingAuth(logger, null); } public static void RunningAuth(this ILogger logger) { runningAuth(logger, null); } public static void AuthSuccessful(this ILogger logger) { authSuccessful(logger, null); } public static void AuthFailed(this ILogger logger) { authFailed(logger, null); } public static void NoConfiguredAuth(this ILogger logger) { noConfiguredAuth(logger, null); } public static void ParsingRequests(this ILogger logger) { parsingRequests(logger, null); } public static void ParsedRequests(this ILogger logger, int count) { parsedRequests(logger, count, null); } public static void ProcessingRequests(this ILogger logger, int count) { processingRequests(logger, count, null); } public static void ResponseFailedWithNoId(this ILogger logger, int code, string message) { responseFailedWithNoId(logger, code, message, null); } public static void NoResponses(this ILogger logger) { noResponses(logger, null); } public static void Responses(this ILogger logger, int count) { responses(logger, count, null); } } }