LoggingMiddleware.cs 6.8 KB

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