You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3KB

  1. using Microsoft.Extensions.Http;
  2. using Microsoft.Extensions.Http.Logging;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace GpsCardGatewayPosition.Postion.HttpLog
  10. {
  11. public class CustomLoggingFilter : IHttpMessageHandlerBuilderFilter
  12. {
  13. private readonly ILoggerFactory _loggerFactory;
  14. public CustomLoggingFilter(ILoggerFactory loggerFactory)
  15. {
  16. _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
  17. }
  18. public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuilder> next)
  19. {
  20. if (next == null)
  21. {
  22. throw new ArgumentNullException(nameof(next));
  23. }
  24. return (builder) =>
  25. {
  26. // Run other configuration first, we want to decorate.
  27. next(builder);
  28. var outerLogger = _loggerFactory.CreateLogger($"HttpClient.{builder.Name}.LogicalHandler");
  29. //builder.AdditionalHandlers.Insert(0, new CustomLoggingScopeHttpMessageHandler(outerLogger));
  30. builder.AdditionalHandlers.Insert(0, new LoggingScopeHttpMessageHandler(outerLogger));
  31. };
  32. }
  33. }
  34. }