LoggingMiddleware.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. dynamic logEnitity = Activator.CreateInstance(entityType, new object[] {
  63. actionName, QueryString, bodyStr, DateTime.Now, "",GetLocalIPAddress(), GetServerIp(context), GetUserExternalIp(context), "", "", msg, error?.InnerException?.Message });
  64. //finally call service.create to insert the log
  65. await service.Create(logEnitity);
  66. //****
  67. switch (error)
  68. {
  69. case AppException e:
  70. {
  71. context.Response.Clear();
  72. context.Response.ContentType = "text/plain";
  73. context.Response.StatusCode = StatusCodes.Status400BadRequest;
  74. await context.Response.WriteAsJsonAsync(
  75. new BadRequestResult
  76. {
  77. ErrorMsg = "*_*" + ((AppException)error).ErrorMessage + "*_*"
  78. });
  79. return;
  80. }
  81. default:
  82. {
  83. context.Response.Clear();
  84. context.Response.ContentType = "text/plain";
  85. context.Response.StatusCode = StatusCodes.Status500InternalServerError;
  86. await context.Response.WriteAsync("*_*" + "Internal Server Error" + "*_*");
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. public static string GetServerIp(HttpContext context)
  93. {
  94. try
  95. {
  96. IPAddress ipAddressString = context.Connection.LocalIpAddress;
  97. string REMOTE_ADDR = context.GetServerVariable("REMOTE_ADDR");
  98. string LOCAL_ADDR = context.GetServerVariable("LOCAL_ADDR");
  99. string SERVER_ADDR = context.GetServerVariable("SERVER_ADDR");
  100. string REMOTE_HOST = context.GetServerVariable("REMOTE_HOST");
  101. string result = "LocalIpAddress: "+ipAddressString.ToString();
  102. result += " REMOTE_ADDR: " + REMOTE_ADDR + " LOCAL_ADDR:" + LOCAL_ADDR
  103. + " SERVER_ADDR:" + SERVER_ADDR + " REMOTE_HOST:" + REMOTE_HOST;
  104. return result;
  105. }
  106. catch (Exception e)
  107. {
  108. return "";
  109. }
  110. }
  111. public static string GetLocalIPAddress()
  112. {
  113. try
  114. {
  115. var host = Dns.GetHostEntry(Dns.GetHostName());
  116. foreach (var ip in host.AddressList)
  117. {
  118. if (ip.AddressFamily == AddressFamily.InterNetwork)
  119. {
  120. return ip.ToString();
  121. }
  122. }
  123. return "";
  124. }
  125. catch (Exception e)
  126. {
  127. return "";
  128. }
  129. }
  130. public static string GetUserExternalIp(HttpContext context)
  131. {
  132. try
  133. {
  134. IPAddress remoteIpAddress = context.Connection.RemoteIpAddress;
  135. string result = "";
  136. if (remoteIpAddress != null)
  137. {
  138. // If we got an IPV6 address, then we need to ask the network for the IPV4 address
  139. // This usually only happens when the browser is on the same machine as the server.
  140. if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  141. {
  142. remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList
  143. .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
  144. }
  145. result = remoteIpAddress.ToString();
  146. }
  147. return result;
  148. }
  149. catch (Exception e)
  150. {
  151. return "";
  152. }
  153. }
  154. }
  155. public class BadRequestResult
  156. {
  157. public string ErrorMsg { get; set; }
  158. public int ErrorNo { get; set; }
  159. }
  160. }