71 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // ***********************************************************************
 | |
| // <copyright file="HttpLoggingMiddleware.cs">
 | |
| //     Heath
 | |
| // </copyright>
 | |
| // ***********************************************************************
 | |
| 
 | |
| using Lib.Common.LoggingAPI.Common.Settings;
 | |
| using Microsoft.AspNetCore.Http;
 | |
| using Microsoft.IO;
 | |
| using Serilog;
 | |
| 
 | |
| namespace Lib.Common.LoggingAPI.Service.Middleware.HttpLogger
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles HTTP logging.
 | |
|     /// </summary>
 | |
|     public class HttpLoggingMiddleware
 | |
|     {
 | |
|         private readonly ILogger logger;
 | |
|         private readonly RequestDelegate requestProcess;
 | |
|         private readonly ServiceSettings settings;
 | |
|         private readonly RecyclableMemoryStreamManager recyclableMemoryStreamManager;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new instrance of <see cref="HttpLoggingMiddleware"/>.
 | |
|         /// </summary>
 | |
|         /// <param name="requestProcess">The request delegate process.</param>
 | |
|         /// <param name="logger">The logger representig an instance of <see cref="ILogger"/>.</param>
 | |
|         /// <param name="settings">The service settings.</param>
 | |
|         public HttpLoggingMiddleware(RequestDelegate requestProcess, ILogger logger, ServiceSettings settings)
 | |
|         {
 | |
|             this.logger = logger;
 | |
|             this.requestProcess = requestProcess;
 | |
|             this.settings = settings;
 | |
|             recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Invoke method.
 | |
|         /// </summary>
 | |
|         /// <param name="context">The HTTP context.</param>
 | |
|         /// <returns></returns>
 | |
|         public async Task Invoke(HttpContext context)
 | |
|         {
 | |
|             await LogRequest(context);
 | |
|             await LogResponse(context);
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Logs an incoming HTTP request.
 | |
|         /// </summary>
 | |
|         /// <param name="context">The HTTP context.</param>
 | |
|         /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 | |
|         private async Task LogRequest(HttpContext context)
 | |
|         {
 | |
|             await this.logger.LogRequest(context, this.recyclableMemoryStreamManager, this.settings.ServiceId);
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Logs an outcome HTTP response.
 | |
|         /// </summary>
 | |
|         /// <param name="context">The HTTP context.</param>
 | |
|         /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 | |
|         private async Task LogResponse(HttpContext context)
 | |
|         {
 | |
|             await this.logger.LogResponse(context, recyclableMemoryStreamManager, requestProcess, this.settings.ServiceId);
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | 
