Add project files.
This commit is contained in:
		
							
								
								
									
										92
									
								
								Core.Blueprint.Logging/Middleware/HttpErrorMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								Core.Blueprint.Logging/Middleware/HttpErrorMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,92 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="HttpErrorMiddleware.cs"> | ||||
| //     Heath | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
|  | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Serilog; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace Core.Blueprint.Logging | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles HTTP logging. | ||||
|     /// </summary> | ||||
|     public class HttpErrorMiddleware | ||||
|     { | ||||
|         private readonly ILogger logger; | ||||
|         private readonly RequestDelegate requestProcess; | ||||
|         public readonly ServiceSettings settings; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new instrance of <see cref="HttpErrorMiddleware"/>. | ||||
|         /// </summary> | ||||
|         /// <param name="logger">The logger representig an instance of <see cref="ILogger"/>.</param> | ||||
|         /// <param name="requestProcess">The request delegate process.</param> | ||||
|         public HttpErrorMiddleware(ILogger logger, RequestDelegate requestProcess, ServiceSettings settings) | ||||
|         { | ||||
|             this.logger = logger; | ||||
|             this.requestProcess = requestProcess; | ||||
|             this.settings = settings; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Invoke method. | ||||
|         /// </summary> | ||||
|         /// <param name="context">The HTTP context.</param> | ||||
|         /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||||
|         public async Task Invoke(HttpContext context) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 await requestProcess(context).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (HttpException exception) | ||||
|             { | ||||
|                 await HandleErrorResponse( | ||||
|                     context, | ||||
|                     exception.Message, | ||||
|                     exception.ErrorCode, | ||||
|                     exception.StatusCode).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception defaultException) | ||||
|             { | ||||
|                 await HandleErrorResponse( | ||||
|                     context, | ||||
|                     defaultException.Message, | ||||
|                     ErrorCodes.InternalServerError, | ||||
|                     StatusCodes.Status500InternalServerError).ConfigureAwait(false); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Handles error responses. | ||||
|         /// </summary> | ||||
|         /// <param name="context">The HTTP context.</param> | ||||
|         /// <param name="message">The error message.</param> | ||||
|         /// <param name="errorCode">The error code.</param> | ||||
|         /// <param name="statusCode">The HTTP status code.</param> | ||||
|         /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||||
|         private async Task HandleErrorResponse(HttpContext context, string? message, string? errorCode, int statusCode) | ||||
|         { | ||||
|             var errorMessage = new HttpError( | ||||
|                 message, | ||||
|                 errorCode, | ||||
|                 string.Format( | ||||
|                     Responses.Target, | ||||
|                     context.Request.Method, | ||||
|                     context.Request.Scheme, | ||||
|                     context.Request.Host.Host, | ||||
|                     context.Request.Path)); | ||||
|  | ||||
|             logger.LogError<HttpError>(context, errorMessage, $"{settings.ApplicationName}-{settings.LayerName}"); | ||||
|  | ||||
|             context.Response.ContentType = MimeTypes.ApplicationJson; | ||||
|             context.Response.StatusCode = statusCode; | ||||
|  | ||||
|             await context.Response.WriteAsync(JsonSerializer.Serialize(errorMessage)).ConfigureAwait(false); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin