颐和api

ActionFilter.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.Filters;
  6. using Microsoft.Extensions.Configuration;
  7. using NLog;
  8. namespace MadRunFabric.Common
  9. {
  10. public class ActionFilter: ActionFilterAttribute
  11. {
  12. IConfiguration _configuration;
  13. public ActionFilter(IConfiguration configuration )
  14. {
  15. _configuration = configuration;
  16. }
  17. public override void OnActionExecuting(ActionExecutingContext context)
  18. {
  19. //try
  20. //{
  21. // if (_configuration["IsLogin"] == "0")
  22. // {
  23. // context.Result = new ContentResult() { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "系统异常,请稍后重试" }.ToJson() };
  24. // return;
  25. // }
  26. //}
  27. //catch
  28. //{
  29. //}
  30. //var dateauth = DateTime.Parse(_configuration["AuthDate"]);
  31. //if ( DateTime.Now> dateauth) {
  32. // context.Result = new ContentResult() { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "授权已到期" }.ToJson() };
  33. // return;
  34. //}
  35. var request = context.HttpContext.Request;
  36. var log = LogManager.GetCurrentClassLogger();
  37. string path = context.ActionDescriptor.AttributeRouteInfo.Template;
  38. if (!string.IsNullOrEmpty(path))
  39. {
  40. log = LogManager.GetLogger(path);
  41. }
  42. bool islog = false;
  43. try
  44. {
  45. if (request.Method == "GET")
  46. {
  47. islog = request.Query.ContainsKey("log");
  48. }
  49. else
  50. {
  51. islog = request.Form.ContainsKey("log");
  52. }
  53. }
  54. catch
  55. {
  56. log.Error("ContentType:" + request.ContentType );
  57. }
  58. if (islog)
  59. {
  60. var ip = request.Headers["X-Forwarded-For"];
  61. if (string.IsNullOrEmpty(ip))
  62. {
  63. ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
  64. }
  65. string userinfo = string.Empty;
  66. var claims = context.HttpContext.User.Claims.GetEnumerator();
  67. while (claims.MoveNext())
  68. {
  69. if (claims.Current.Type.IndexOf("/") >= 0)
  70. {
  71. userinfo += claims.Current.Type.Substring(claims.Current.Type.LastIndexOf('/') + 1) + ":" + claims.Current.Value + ";";
  72. }
  73. }
  74. var param = string.Empty;
  75. if (request.Method == "GET")
  76. {
  77. param = request.Query.ToJson();
  78. }
  79. else
  80. {
  81. param = request.Form.ToJson();
  82. }
  83. log.Error("IP:" + ip.ToString() + "|||UserInfo:" + userinfo + "|||ContentType:" + request.ContentType + "|||Params:" + param);
  84. }
  85. base.OnActionExecuting(context);
  86. }
  87. public override void OnActionExecuted(ActionExecutedContext context)
  88. {
  89. var request = context.HttpContext.Request;
  90. bool islog = false;
  91. try
  92. {
  93. if (request.Method == "GET")
  94. {
  95. islog = request.Query.ContainsKey("log");
  96. }
  97. else
  98. {
  99. islog = request.Form.ContainsKey("log");
  100. }
  101. }
  102. catch
  103. {
  104. islog = true;
  105. }
  106. if (islog)
  107. {
  108. var log = LogManager.GetCurrentClassLogger();
  109. string path = context.ActionDescriptor.AttributeRouteInfo.Template;
  110. if (!string.IsNullOrEmpty(path))
  111. {
  112. log = LogManager.GetLogger(path);
  113. }
  114. log.Error(context.Result.ToJson());
  115. }
  116. base.OnActionExecuted(context);
  117. }
  118. }
  119. }