Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

100 lines
3.7KB

  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace HealthMonitor.WebApi.HttpLog
  11. {
  12. public class CustomLoggingScopeHttpMessageHandler : DelegatingHandler
  13. {
  14. private readonly ILogger _logger;
  15. public CustomLoggingScopeHttpMessageHandler(ILogger logger)
  16. {
  17. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  18. }
  19. protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
  20. CancellationToken cancellationToken)
  21. {
  22. if (request == null)
  23. {
  24. throw new ArgumentNullException(nameof(request));
  25. }
  26. using (Log.BeginRequestPipelineScope(_logger, request))
  27. {
  28. Log.RequestPipelineStart(_logger, request);
  29. var response = await base.SendAsync(request, cancellationToken);
  30. Log.RequestPipelineEnd(_logger, response);
  31. return response;
  32. }
  33. }
  34. private static class Log
  35. {
  36. private static class EventIds
  37. {
  38. public static readonly EventId PipelineStart = new EventId(100, "RequestPipelineStart");
  39. public static readonly EventId PipelineEnd = new EventId(101, "RequestPipelineEnd");
  40. }
  41. private static readonly Func<ILogger, HttpMethod, Uri, string, IDisposable?> _beginRequestPipelineScope =
  42. LoggerMessage.DefineScope<HttpMethod, Uri, string>(
  43. "HTTP {HttpMethod} {Uri} {CorrelationId}");
  44. private static readonly Action<ILogger, HttpMethod, Uri, string, Exception?> _requestPipelineStart =
  45. LoggerMessage.Define<HttpMethod, Uri, string>(
  46. LogLevel.Information,
  47. EventIds.PipelineStart,
  48. "Start processing HTTP request {HttpMethod} {Uri} [Correlation: {CorrelationId}]");
  49. private static readonly Action<ILogger, HttpStatusCode, Exception?> _requestPipelineEnd =
  50. LoggerMessage.Define<HttpStatusCode>(
  51. LogLevel.Information,
  52. EventIds.PipelineEnd,
  53. "End processing HTTP request - {StatusCode}");
  54. public static IDisposable BeginRequestPipelineScope(ILogger logger, HttpRequestMessage request)
  55. {
  56. var correlationId = GetCorrelationIdFromRequest(request);
  57. return _beginRequestPipelineScope(logger, request.Method, request.RequestUri!, correlationId)!;
  58. }
  59. public static void RequestPipelineStart(ILogger logger, HttpRequestMessage request)
  60. {
  61. if (logger.IsEnabled(LogLevel.Trace))
  62. {
  63. var correlationId = GetCorrelationIdFromRequest(request);
  64. _requestPipelineStart(logger, request.Method, request.RequestUri!, correlationId, null);
  65. }
  66. }
  67. public static void RequestPipelineEnd(ILogger logger, HttpResponseMessage response)
  68. {
  69. if (logger.IsEnabled(LogLevel.Trace))
  70. {
  71. _requestPipelineEnd(logger, response.StatusCode, null);
  72. }
  73. }
  74. private static string GetCorrelationIdFromRequest(HttpRequestMessage request)
  75. {
  76. var correlationId = "Not set";
  77. if (request.Headers.TryGetValues("X-Correlation-ID", out var values))
  78. {
  79. correlationId = values.First();
  80. }
  81. return correlationId;
  82. }
  83. }
  84. }
  85. }