LogHandler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. using System.Diagnostics;
  2. using SHJX.Service.Common.Logging;
  3. using Microsoft.Extensions.Logging;
  4. using Unity.Interception.PolicyInjection.Pipeline;
  5. namespace SHJX.Service.Common.Interceptor.Handlers
  6. {
  7. [DebuggerStepThrough]
  8. public class LogHandler : ICallHandler
  9. {
  10. public int Order { get; set; }
  11. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(LogHandler));
  12. public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
  13. {
  14. logger.LogInformation("-------------Method Excute Befored-------------");
  15. logger.LogInformation($"Method Name:{input.MethodBase.Name}");
  16. if (input.Arguments.Count > 0)
  17. {
  18. logger.LogInformation("Arguments:");
  19. for (int i = 0; i < input.Arguments.Count; i++)
  20. {
  21. logger.LogInformation($"parameterName:{input.Arguments.ParameterName(i)},parameterValue:{input.Arguments[i]}");
  22. }
  23. }
  24. var methodReturn = getNext()(input, getNext);
  25. logger.LogInformation("-------------Method Excute After-------------");
  26. logger.LogInformation(methodReturn.Exception is not null ? $"Exception:{methodReturn.Exception.Message} \n" : $"Excuted Successed \n");
  27. return methodReturn;
  28. }
  29. }
  30. }