LoggingMiddleware.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.Data.SqlClient;
  4. using Microsoft.Extensions.Primitives;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using Microsoft.AspNetCore.Mvc;
  15. using MTWorkHR.Core.UnitOfWork;
  16. using MTWorkHR.Application.Services;
  17. using MTWorkHR.Core.Global;
  18. using Microsoft.Extensions.Logging;
  19. namespace MTWorkHR.Application.Middlewares
  20. {
  21. public class LoggingMiddleware
  22. {
  23. private readonly RequestDelegate _next;
  24. private readonly ILogger<LoggingMiddleware> _logger;
  25. public LoggingMiddleware(RequestDelegate next, ILogger<LoggingMiddleware> logger)
  26. {
  27. _next = next;
  28. _logger = logger;
  29. }
  30. public async Task Invoke(HttpContext context, IUnitOfWorkLog unitOfWork)
  31. {
  32. try
  33. {
  34. await _next(context);
  35. }
  36. catch (Exception error)
  37. {
  38. //Get target controller info
  39. _logger.LogError(error, "An error occurred during request processing");
  40. var controllerActionDescriptor = context?
  41. .GetEndpoint()?
  42. .Metadata
  43. .GetMetadata<ControllerActionDescriptor>();
  44. //get controllerName & actionName
  45. var controllerName = controllerActionDescriptor?.ControllerName;
  46. var actionName = controllerActionDescriptor?.ActionName;
  47. //get QueryString
  48. var req = context?.Request;
  49. var QueryString = req?.QueryString.Value?.ToString();
  50. // get request body
  51. string bodyStr;
  52. req.EnableBuffering();
  53. req.Body.Seek(0, SeekOrigin.Begin);
  54. req.Body.Position = 0;
  55. using (StreamReader reader
  56. = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
  57. {
  58. bodyStr = await reader.ReadToEndAsync();
  59. }
  60. //**Get Log service and entity info by refliction
  61. //Get target log entity by the traget controller name
  62. Type? entityType = Type.GetType("MTWorkHR.Core.Entities." + "User" + "Log, MTWorkHR.Core");
  63. var logServiceWithGenericType = typeof(LogService<>).MakeGenericType(entityType);
  64. dynamic service = Activator.CreateInstance(logServiceWithGenericType, new object[] { unitOfWork });
  65. var msg = error is AppException ? ((AppException)error).ErrorMessage : error.Message;
  66. var errorNo = error is AppException ? ((AppException)error).ErrorNumber : "0";
  67. int errorNum = 0;
  68. int.TryParse(errorNo, out errorNum);
  69. dynamic logEnitity = Activator.CreateInstance(entityType, new object[] {
  70. controllerName+"/"+actionName, QueryString, bodyStr, DateTime.Now, "",GetLocalIPAddress(), GetServerIp(context), GetUserExternalIp(context), "", "", msg, error?.InnerException?.Message });
  71. //finally call service.create to insert the log
  72. await service.Create(logEnitity);
  73. //****
  74. switch (error)
  75. {
  76. case AppException e:
  77. {
  78. context.Response.Clear();
  79. context.Response.ContentType = "text/plain";
  80. context.Response.StatusCode = StatusCodes.Status400BadRequest;
  81. await context.Response.WriteAsJsonAsync(
  82. new BadRequestResult
  83. {
  84. ErrorMsg = "*_*" + ((AppException)error).ErrorMessage + "*_*",
  85. ErrorNo = errorNum
  86. });
  87. return;
  88. }
  89. default:
  90. {
  91. context.Response.Clear();
  92. context.Response.ContentType = "text/plain";
  93. context.Response.StatusCode = StatusCodes.Status500InternalServerError;
  94. await context.Response.WriteAsync("*_*" + "Internal Server Error" + "*_*");
  95. return;
  96. }
  97. }
  98. }
  99. }
  100. public static string GetServerIp(HttpContext context)
  101. {
  102. try
  103. {
  104. IPAddress ipAddressString = context.Connection.LocalIpAddress;
  105. string REMOTE_ADDR = context.GetServerVariable("REMOTE_ADDR");
  106. string LOCAL_ADDR = context.GetServerVariable("LOCAL_ADDR");
  107. string SERVER_ADDR = context.GetServerVariable("SERVER_ADDR");
  108. string REMOTE_HOST = context.GetServerVariable("REMOTE_HOST");
  109. string result = "LocalIpAddress: "+ipAddressString.ToString();
  110. result += " REMOTE_ADDR: " + REMOTE_ADDR + " LOCAL_ADDR:" + LOCAL_ADDR
  111. + " SERVER_ADDR:" + SERVER_ADDR + " REMOTE_HOST:" + REMOTE_HOST;
  112. return result;
  113. }
  114. catch (Exception e)
  115. {
  116. return "";
  117. }
  118. }
  119. public static string GetLocalIPAddress()
  120. {
  121. try
  122. {
  123. var host = Dns.GetHostEntry(Dns.GetHostName());
  124. foreach (var ip in host.AddressList)
  125. {
  126. if (ip.AddressFamily == AddressFamily.InterNetwork)
  127. {
  128. return ip.ToString();
  129. }
  130. }
  131. return "";
  132. }
  133. catch (Exception e)
  134. {
  135. return "";
  136. }
  137. }
  138. public static string GetUserExternalIp(HttpContext context)
  139. {
  140. try
  141. {
  142. IPAddress remoteIpAddress = context.Connection.RemoteIpAddress;
  143. string result = "";
  144. if (remoteIpAddress != null)
  145. {
  146. // If we got an IPV6 address, then we need to ask the network for the IPV4 address
  147. // This usually only happens when the browser is on the same machine as the server.
  148. if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  149. {
  150. remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList
  151. .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
  152. }
  153. result = remoteIpAddress.ToString();
  154. }
  155. return result;
  156. }
  157. catch (Exception e)
  158. {
  159. return "";
  160. }
  161. }
  162. }
  163. public class BadRequestResult
  164. {
  165. public string ErrorMsg { get; set; }
  166. public int ErrorNo { get; set; }
  167. }
  168. }