LoggerExtensions.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using JsonRPC4.Common;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace JsonRPC4.Router.Utilities
  9. {
  10. public static class LoggerExtensions
  11. {
  12. private static readonly Action<ILogger, string, Exception> attemptingToMatchMethod;
  13. private static readonly Action<ILogger, Exception> requestMatchedMethod;
  14. private static readonly Action<ILogger, string, Exception> methodsInRoute;
  15. private const LogLevel methodsInRouteLevel = LogLevel.Trace;
  16. private static readonly Action<ILogger, int, Exception> invokingBatchRequests;
  17. private static readonly Action<ILogger, Exception> batchRequestsComplete;
  18. private static readonly Action<ILogger, RpcId, Exception> invokingRequest;
  19. private static readonly Action<ILogger, string, Exception> invokeMethod;
  20. private static readonly Action<ILogger, string, Exception> invokeMethodComplete;
  21. private static readonly Action<ILogger, RpcId, Exception> finishedRequest;
  22. private static readonly Action<ILogger, Exception> finishedRequestNoId;
  23. private static readonly Action<ILogger, Exception> skippingAuth;
  24. private static readonly Action<ILogger, Exception> runningAuth;
  25. private static readonly Action<ILogger, Exception> authSuccessful;
  26. private static readonly Action<ILogger, Exception> authFailed;
  27. private static readonly Action<ILogger, Exception> noConfiguredAuth;
  28. private static readonly Action<ILogger, Exception> parsingRequests;
  29. private static readonly Action<ILogger, int, Exception> parsedRequests;
  30. private static readonly Action<ILogger, int, Exception> processingRequests;
  31. private static readonly Action<ILogger, int, string, Exception> responseFailedWithNoId;
  32. private static readonly Action<ILogger, Exception> noResponses;
  33. private static readonly Action<ILogger, int, Exception> responses;
  34. static LoggerExtensions()
  35. {
  36. attemptingToMatchMethod = LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AttemptingToMatchMethod"), "Attempting to match Rpc request to a method '{Method}'");
  37. requestMatchedMethod = LoggerMessage.Define(LogLevel.Debug, new EventId(2, "RequestMatchedMethod"), "Request was matched to a method");
  38. methodsInRoute = LoggerMessage.Define<string>(LogLevel.Trace, new EventId(3, "MethodsInRoute"), "Methods in route: {MethodsString}");
  39. invokingBatchRequests = LoggerMessage.Define<int>(LogLevel.Debug, new EventId(4, "InvokingBatchRequests"), "Invoking '{Count}' batch requests");
  40. batchRequestsComplete = LoggerMessage.Define(LogLevel.Debug, new EventId(5, "BatchRequestsComplete"), "Finished batch requests");
  41. invokingRequest = LoggerMessage.Define<RpcId>(LogLevel.Debug, new EventId(6, "InvokingRequest"), "Invoking request with id '{Id}'");
  42. invokeMethod = LoggerMessage.Define<string>(LogLevel.Debug, new EventId(7, "InvokeMethod"), "Attempting to invoke method '{Method}'");
  43. invokeMethodComplete = LoggerMessage.Define<string>(LogLevel.Debug, new EventId(8, "InvokeMethodComplete"), "Finished invoking method '{Method}'");
  44. finishedRequest = LoggerMessage.Define<RpcId>(LogLevel.Debug, new EventId(9, "FinishedRequest"), "Finished request with id: {Id}");
  45. finishedRequestNoId = LoggerMessage.Define(LogLevel.Debug, new EventId(10, "FinishedRequestNoId"), "Finished request with no id. Not returning a response");
  46. skippingAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(11, "SkippingAuth"), "Skipping authorization. Allow anonymous specified for method.");
  47. runningAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(12, "RunningAuth"), "Running authorization for method.");
  48. authSuccessful = LoggerMessage.Define(LogLevel.Debug, new EventId(13, "AuthSuccessful"), "Authorization was successful.");
  49. authFailed = LoggerMessage.Define(LogLevel.Information, new EventId(14, "AuthFailed"), "Authorization failed.");
  50. noConfiguredAuth = LoggerMessage.Define(LogLevel.Debug, new EventId(15, "NoConfiguredAuth"), "Skipping authorization. None configured for class or method.");
  51. parsingRequests = LoggerMessage.Define(LogLevel.Debug, new EventId(16, "ParsingRequests"), "Attempting to parse Rpc request from the json");
  52. parsedRequests = LoggerMessage.Define<int>(LogLevel.Debug, new EventId(17, "ParsedRequests"), "Successfully parsed {Count} Rpc request(s)");
  53. processingRequests = LoggerMessage.Define<int>(LogLevel.Information, new EventId(18, "ProcessingRequests"), "Processing {Count} Rpc requests");
  54. 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}");
  55. noResponses = LoggerMessage.Define(LogLevel.Information, new EventId(20, "NoResponses"), "No rpc responses created.");
  56. responses = LoggerMessage.Define<int>(LogLevel.Information, new EventId(21, "Responses"), "{Count} rpc response(s) created.");
  57. }
  58. public static void LogException(this ILogger logger, Exception ex, string message = null)
  59. {
  60. message = ((message == null) ? $"{ex}" : $"{message}{Environment.NewLine}{ex}");
  61. logger.LogError(default(EventId), ex, message);
  62. }
  63. public static void AttemptingToMatchMethod(this ILogger logger, string method)
  64. {
  65. attemptingToMatchMethod(logger, method, null);
  66. }
  67. public static void RequestMatchedMethod(this ILogger logger)
  68. {
  69. requestMatchedMethod(logger, null);
  70. }
  71. public static void MethodsInRoute(this ILogger logger, IEnumerable<MethodInfo> methods)
  72. {
  73. if (logger.IsEnabled(LogLevel.Trace))
  74. {
  75. string arg = string.Join(", ", methods.Select((MethodInfo m) => m.Name));
  76. methodsInRoute(logger, arg, null);
  77. }
  78. }
  79. public static void InvokingBatchRequests(this ILogger logger, int count)
  80. {
  81. invokingBatchRequests(logger, count, null);
  82. }
  83. public static void BatchRequestsComplete(this ILogger logger)
  84. {
  85. batchRequestsComplete(logger, null);
  86. }
  87. public static void InvokingRequest(this ILogger logger, RpcId id)
  88. {
  89. invokingRequest(logger, id, null);
  90. }
  91. public static void InvokeMethod(this ILogger logger, string method)
  92. {
  93. invokeMethod(logger, method, null);
  94. }
  95. public static void InvokeMethodComplete(this ILogger logger, string method)
  96. {
  97. invokeMethodComplete(logger, method, null);
  98. }
  99. public static void FinishedRequest(this ILogger logger, RpcId id)
  100. {
  101. finishedRequest(logger, id, null);
  102. }
  103. public static void FinishedRequestNoId(this ILogger logger)
  104. {
  105. finishedRequestNoId(logger, null);
  106. }
  107. public static void SkippingAuth(this ILogger logger)
  108. {
  109. skippingAuth(logger, null);
  110. }
  111. public static void RunningAuth(this ILogger logger)
  112. {
  113. runningAuth(logger, null);
  114. }
  115. public static void AuthSuccessful(this ILogger logger)
  116. {
  117. authSuccessful(logger, null);
  118. }
  119. public static void AuthFailed(this ILogger logger)
  120. {
  121. authFailed(logger, null);
  122. }
  123. public static void NoConfiguredAuth(this ILogger logger)
  124. {
  125. noConfiguredAuth(logger, null);
  126. }
  127. public static void ParsingRequests(this ILogger logger)
  128. {
  129. parsingRequests(logger, null);
  130. }
  131. public static void ParsedRequests(this ILogger logger, int count)
  132. {
  133. parsedRequests(logger, count, null);
  134. }
  135. public static void ProcessingRequests(this ILogger logger, int count)
  136. {
  137. processingRequests(logger, count, null);
  138. }
  139. public static void ResponseFailedWithNoId(this ILogger logger, int code, string message)
  140. {
  141. responseFailedWithNoId(logger, code, message, null);
  142. }
  143. public static void NoResponses(this ILogger logger)
  144. {
  145. noResponses(logger, null);
  146. }
  147. public static void Responses(this ILogger logger, int count)
  148. {
  149. responses(logger, count, null);
  150. }
  151. }
  152. }