123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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<ILogger, string, Exception> attemptingToMatchMethod;
- private static readonly Action<ILogger, Exception> requestMatchedMethod;
- private static readonly Action<ILogger, string, Exception> methodsInRoute;
- private const LogLevel methodsInRouteLevel = LogLevel.Trace;
- private static readonly Action<ILogger, int, Exception> invokingBatchRequests;
- private static readonly Action<ILogger, Exception> batchRequestsComplete;
- private static readonly Action<ILogger, RpcId, Exception> invokingRequest;
- private static readonly Action<ILogger, string, Exception> invokeMethod;
- private static readonly Action<ILogger, string, Exception> invokeMethodComplete;
- private static readonly Action<ILogger, RpcId, Exception> finishedRequest;
- private static readonly Action<ILogger, Exception> finishedRequestNoId;
- private static readonly Action<ILogger, Exception> skippingAuth;
- private static readonly Action<ILogger, Exception> runningAuth;
- private static readonly Action<ILogger, Exception> authSuccessful;
- private static readonly Action<ILogger, Exception> authFailed;
- private static readonly Action<ILogger, Exception> noConfiguredAuth;
- private static readonly Action<ILogger, Exception> parsingRequests;
- private static readonly Action<ILogger, int, Exception> parsedRequests;
- private static readonly Action<ILogger, int, Exception> processingRequests;
- private static readonly Action<ILogger, int, string, Exception> responseFailedWithNoId;
- private static readonly Action<ILogger, Exception> noResponses;
- private static readonly Action<ILogger, int, Exception> responses;
- static LoggerExtensions()
- {
- attemptingToMatchMethod = LoggerMessage.Define<string>(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<string>(LogLevel.Trace, new EventId(3, "MethodsInRoute"), "Methods in route: {MethodsString}");
- invokingBatchRequests = LoggerMessage.Define<int>(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<RpcId>(LogLevel.Debug, new EventId(6, "InvokingRequest"), "Invoking request with id '{Id}'");
- invokeMethod = LoggerMessage.Define<string>(LogLevel.Debug, new EventId(7, "InvokeMethod"), "Attempting to invoke method '{Method}'");
- invokeMethodComplete = LoggerMessage.Define<string>(LogLevel.Debug, new EventId(8, "InvokeMethodComplete"), "Finished invoking method '{Method}'");
- finishedRequest = LoggerMessage.Define<RpcId>(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<int>(LogLevel.Debug, new EventId(17, "ParsedRequests"), "Successfully parsed {Count} Rpc request(s)");
- processingRequests = LoggerMessage.Define<int>(LogLevel.Information, new EventId(18, "ProcessingRequests"), "Processing {Count} Rpc requests");
- responseFailedWithNoId = LoggerMessage.Define<int, string>(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<int>(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<MethodInfo> 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);
- }
- }
- }
|