mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:23:26 +08:00
# 2022-04-10
### 1.4.4开源社区版: 无 ### 0.3.5 专业版: 【新增】增加用户操作日志,可记录接口也可记录后台,同时支持本地存储或数据库存储。支持配置文件开启。 【新增】数据库增加用户操作日志表。 【优化】优化反射获取所有Controller 和Action的全局方法,增加缓存设置。 【优化】优化记录IP请求数据的中间件。 【修复】修复ios下用户充值余额的功能不显示的情况。 【修复】修复我的余额面板列表中右侧三角无反应的问题。 【修复】修复代理中心下线人数和订单数量统计错误的问题。#I51OUC
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
<ProjectReference Include="..\CoreCms.Net.Auth\CoreCms.Net.Auth.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Configuration\CoreCms.Net.Configuration.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Core\CoreCms.Net.Core.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Filter\CoreCms.Net.Filter.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.IServices\CoreCms.Net.IServices.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Loging\CoreCms.Net.Loging.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Utility\CoreCms.Net.Utility.csproj" />
|
||||
|
||||
@@ -13,60 +13,57 @@
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace CoreCms.Net.Middlewares
|
||||
namespace CoreCms.Net.Middlewares;
|
||||
|
||||
/// <summary>
|
||||
/// 异常错误统一返回记录
|
||||
/// </summary>
|
||||
public class ExceptionHandlerMiddForAdmin
|
||||
{
|
||||
/// <summary>
|
||||
/// 异常错误统一返回记录
|
||||
/// </summary>
|
||||
public class ExceptionHandlerMiddForAdmin
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public ExceptionHandlerMiddForAdmin(RequestDelegate next)
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public ExceptionHandlerMiddForAdmin(RequestDelegate next)
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
_next = next;
|
||||
await _next(context);
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
catch (Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception ex)
|
||||
{
|
||||
if (ex == null) return;
|
||||
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.Web, "全局捕获异常", "全局捕获异常", new Exception("全局捕获异常", ex));
|
||||
await WriteExceptionAsync(context, ex).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task WriteExceptionAsync(HttpContext context, Exception e)
|
||||
{
|
||||
if (e is UnauthorizedAccessException) context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
else if (e is Exception) context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
var jm = new AdminUiCallBack();
|
||||
jm.code = 500;
|
||||
jm.data = e;
|
||||
jm.msg = "全局捕获异常";
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(jm)).ConfigureAwait(false);
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception ex)
|
||||
{
|
||||
if (ex == null) return;
|
||||
NLogUtil.WriteAll(LogLevel.Error, LogType.Web, "全局捕获异常", "全局捕获异常", new Exception("全局捕获异常", ex));
|
||||
await WriteExceptionAsync(context, ex).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task WriteExceptionAsync(HttpContext context, Exception e)
|
||||
{
|
||||
if (e is UnauthorizedAccessException) context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
else if (e != null) context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
var jm = new AdminUiCallBack();
|
||||
jm.code = 500;
|
||||
jm.data = e;
|
||||
jm.msg = "全局捕获异常";
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(jm)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -13,69 +13,63 @@
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace CoreCms.Net.Middlewares
|
||||
namespace CoreCms.Net.Middlewares;
|
||||
|
||||
/// <summary>
|
||||
/// 异常错误统一返回记录
|
||||
/// </summary>
|
||||
public class ExceptionHandlerMiddForClent
|
||||
{
|
||||
/// <summary>
|
||||
/// 异常错误统一返回记录
|
||||
/// </summary>
|
||||
public class ExceptionHandlerMiddForClent
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
|
||||
public ExceptionHandlerMiddForClent(RequestDelegate next)
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
_next = next;
|
||||
}
|
||||
|
||||
|
||||
public ExceptionHandlerMiddForClent(RequestDelegate next)
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
_next = next;
|
||||
await _next(context);
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
catch (Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception ex)
|
||||
{
|
||||
if (ex == null) return;
|
||||
|
||||
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.ApiRequest, "全局捕获异常", "全局捕获异常", new Exception("全局捕获异常", ex));
|
||||
|
||||
await WriteExceptionAsync(context, ex).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task WriteExceptionAsync(HttpContext context, Exception e)
|
||||
{
|
||||
if (e is UnauthorizedAccessException)
|
||||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
else if (e is Exception)
|
||||
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
var jm = new WebApiCallBack();
|
||||
jm.status = false;
|
||||
jm.code = 500;
|
||||
jm.data = e;
|
||||
jm.msg = "全局数据异常";
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(jm)).ConfigureAwait(false);
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception ex)
|
||||
{
|
||||
if (ex == null) return;
|
||||
|
||||
NLogUtil.WriteAll(LogLevel.Error, LogType.ApiRequest, "全局捕获异常", "全局捕获异常", new Exception("全局捕获异常", ex));
|
||||
|
||||
await WriteExceptionAsync(context, ex).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task WriteExceptionAsync(HttpContext context, Exception e)
|
||||
{
|
||||
if (e is UnauthorizedAccessException)
|
||||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
else if (e != null)
|
||||
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
var jm = new WebApiCallBack();
|
||||
jm.status = false;
|
||||
jm.code = 500;
|
||||
jm.data = e;
|
||||
jm.msg = "全局数据异常";
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(jm)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -67,9 +67,9 @@ namespace CoreCms.Net.Middlewares
|
||||
|
||||
if (!string.IsNullOrEmpty(requestInfo))
|
||||
{
|
||||
Parallel.For(0, 1, e =>
|
||||
Parallel.For(0, 1, _ =>
|
||||
{
|
||||
LogLockHelper.OutSql2Log("RequestIpInfoLog", "RequestIpInfoLog" + dt.ToString("yyyy-MM-dd-HH"), new string[] { requestInfo + "," }, false);
|
||||
LogLockHelper.OutSql2Log("RequestIpInfoLog", "RequestIpInfoLog" + dt.ToString("yyyy-MM-dd-HH"), new[] { requestInfo + "," }, false);
|
||||
});
|
||||
request.Body.Position = 0;
|
||||
}
|
||||
@@ -77,6 +77,7 @@ namespace CoreCms.Net.Middlewares
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -95,10 +96,7 @@ namespace CoreCms.Net.Middlewares
|
||||
var ip = context.Request.Headers["X-Forwarded-For"].ObjectToString();
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
{
|
||||
if (context.Connection.RemoteIpAddress != null)
|
||||
{
|
||||
ip = context.Connection.RemoteIpAddress.MapToIPv4().ObjectToString();
|
||||
}
|
||||
ip = context.Connection.RemoteIpAddress.ObjectToString();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
@@ -78,10 +78,11 @@ namespace CoreCms.Net.Middlewares
|
||||
/// 用户访问接口日志中间件
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="dataSources"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app)
|
||||
public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app,string dataSources)
|
||||
{
|
||||
return app.UseMiddleware<RecordAccessLogsMildd>();
|
||||
return app.UseMiddleware<RecordAccessLogsMildd>(dataSources);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,15 +3,24 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using CoreCms.Net.Auth.HttpContextUser;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Filter;
|
||||
using CoreCms.Net.IServices;
|
||||
using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.ViewModels.Basics;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
|
||||
namespace CoreCms.Net.Middlewares
|
||||
{
|
||||
@@ -21,103 +30,143 @@ namespace CoreCms.Net.Middlewares
|
||||
/// </summary>
|
||||
public class RecordAccessLogsMildd
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IHttpContextUser _user;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ISysUserOperationLogServices _sysUserOperationLogServices;
|
||||
|
||||
private readonly ILogger<RecordAccessLogsMildd> _logger;
|
||||
private readonly Stopwatch _stopwatch;
|
||||
|
||||
private readonly string _dataSources;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 构造
|
||||
/// </summary>
|
||||
/// <param name="next"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="logger"></param>
|
||||
public RecordAccessLogsMildd(RequestDelegate next, IHttpContextUser user, ILogger<RecordAccessLogsMildd> logger)
|
||||
public RecordAccessLogsMildd(RequestDelegate next, IHttpContextAccessor httpContextAccessor, ISysUserOperationLogServices sysUserOperationLogServices, string dataSources)
|
||||
{
|
||||
_next = next;
|
||||
_user = user;
|
||||
_logger = logger;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_dataSources = dataSources;
|
||||
_sysUserOperationLogServices = sysUserOperationLogServices;
|
||||
_stopwatch = new Stopwatch();
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (AppSettingsConstVars.MiddlewareRecordAccessLogsEnabled)
|
||||
//用户访问记录-是否开启
|
||||
var middlewareRecordAccessLogsEnabled = AppSettingsConstVars.MiddlewareRecordAccessLogsEnabled;
|
||||
//是否开启记录到文件模式
|
||||
var middlewareRecordAccessLogsEnabledFileMode = AppSettingsConstVars.MiddlewareRecordAccessLogsEnabledFileMode;
|
||||
//是否开启记录到数据库模式
|
||||
var middlewareRecordAccessLogsEnabledDbMode = AppSettingsConstVars.MiddlewareRecordAccessLogsEnabledDbMode;
|
||||
|
||||
if (middlewareRecordAccessLogsEnabled && (middlewareRecordAccessLogsEnabledFileMode || middlewareRecordAccessLogsEnabledDbMode))
|
||||
{
|
||||
var api = context.Request.Path.ObjectToString().TrimEnd('/').ToLower();
|
||||
var request = context.Request;
|
||||
var api = request.Path.ObjectToString().TrimEnd('/').ToLower();
|
||||
var ignoreApis = AppSettingsConstVars.MiddlewareRecordAccessLogsIgnoreApis;
|
||||
|
||||
// 过滤,只有接口
|
||||
if (api.Contains("api") && !ignoreApis.Contains(api))
|
||||
if (api.Contains("api") && !ignoreApis.ToLower().Contains(api))
|
||||
{
|
||||
_stopwatch.Restart();
|
||||
var userAccessModel = new UserAccessModel();
|
||||
var uLog = new SysUserOperationLog();
|
||||
|
||||
HttpRequest request = context.Request;
|
||||
|
||||
userAccessModel.API = api;
|
||||
userAccessModel.User = _user.Name;
|
||||
userAccessModel.IP = IPLogMildd.GetClientIp(context);
|
||||
userAccessModel.BeginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
userAccessModel.RequestMethod = request.Method;
|
||||
userAccessModel.Agent = request.Headers["User-Agent"].ObjectToString();
|
||||
|
||||
|
||||
// 获取请求body内容
|
||||
if (request.Method.ToLower().Equals("post") || request.Method.ToLower().Equals("put"))
|
||||
if (_httpContextAccessor.HttpContext != null)
|
||||
{
|
||||
// 启用倒带功能,就可以让 Request.Body 可以再次读取
|
||||
request.EnableBuffering();
|
||||
var authenticate = await _httpContextAccessor.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
|
||||
if (authenticate.Succeeded && authenticate.Principal != null)
|
||||
{
|
||||
//账号
|
||||
uLog.userName = authenticate.Principal.Identities.First().FindFirst(ClaimTypes.Name)?.Value;
|
||||
//昵称
|
||||
uLog.userNickName = authenticate.Principal.Identities.First().FindFirst(ClaimTypes.GivenName)?.Value;
|
||||
//用户序列
|
||||
uLog.userId = authenticate.Principal.Identities.First().FindFirst(JwtRegisteredClaimNames.Jti)!.Value.ObjectToInt(0);
|
||||
}
|
||||
|
||||
Stream stream = request.Body;
|
||||
byte[] buffer = new byte[request.ContentLength.Value];
|
||||
stream.Read(buffer, 0, buffer.Length);
|
||||
userAccessModel.RequestData = Encoding.UTF8.GetString(buffer);
|
||||
var adminLog = AdminsControllerPermission.GetCacheCoreCmsActionPermission();
|
||||
|
||||
request.Body.Position = 0;
|
||||
var curLog = adminLog.FirstOrDefault(p => p.path.ToLower() == _httpContextAccessor.HttpContext.Request.Path.Value.ToLower());
|
||||
if (curLog != null)
|
||||
{
|
||||
uLog.actionName = curLog.actionName;
|
||||
uLog.actionDescription = curLog.description;
|
||||
uLog.controllerName = curLog.controllerName;
|
||||
uLog.controllerDescription = curLog.controllerDescription;
|
||||
}
|
||||
uLog.apiPath = _httpContextAccessor.HttpContext.Request.Path.Value;
|
||||
uLog.statusCode = _httpContextAccessor.HttpContext.Response.StatusCode;
|
||||
}
|
||||
else if (request.Method.ToLower().Equals("get") || request.Method.ToLower().Equals("delete"))
|
||||
else
|
||||
{
|
||||
userAccessModel.RequestData = HttpUtility.UrlDecode(request.QueryString.ObjectToString(), Encoding.UTF8);
|
||||
uLog.apiPath = api;
|
||||
}
|
||||
|
||||
uLog.ip = IPLogMildd.GetClientIp(context);
|
||||
uLog.beginTime = DateTime.Now;
|
||||
uLog.requestMethod = request.Method;
|
||||
uLog.agent = request.Headers["User-Agent"].ObjectToString();
|
||||
uLog.dataSources = _dataSources;
|
||||
|
||||
switch (request.Method.ToLower())
|
||||
{
|
||||
// 获取请求body内容
|
||||
case "post":
|
||||
case "put":
|
||||
{
|
||||
// 启用倒带功能,就可以让 Request.Body 可以再次读取
|
||||
request.EnableBuffering();
|
||||
var stream = request.Body;
|
||||
if (request.ContentLength != null)
|
||||
{
|
||||
byte[] buffer = new byte[request.ContentLength.Value];
|
||||
var read = await stream.ReadAsync(buffer);
|
||||
uLog.requestData = Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
request.Body.Position = 0;
|
||||
break;
|
||||
}
|
||||
case "get":
|
||||
case "delete":
|
||||
uLog.requestData = HttpUtility.UrlDecode(request.QueryString.ObjectToString(), Encoding.UTF8);
|
||||
break;
|
||||
}
|
||||
|
||||
// 获取Response.Body内容
|
||||
var originalBodyStream = context.Response.Body;
|
||||
using (var responseBody = new MemoryStream())
|
||||
await using (var responseBody = new MemoryStream())
|
||||
{
|
||||
context.Response.Body = responseBody;
|
||||
|
||||
await _next(context);
|
||||
|
||||
var responseBodyData = await GetResponse(context.Response);
|
||||
|
||||
uLog.responseBodyData = await GetResponse(context.Response);
|
||||
await responseBody.CopyToAsync(originalBodyStream);
|
||||
}
|
||||
|
||||
|
||||
var dt = DateTime.Now;
|
||||
|
||||
// 响应完成记录时间和存入日志
|
||||
context.Response.OnCompleted(() =>
|
||||
context.Response.OnCompleted(async () =>
|
||||
{
|
||||
_stopwatch.Stop();
|
||||
|
||||
userAccessModel.OPTime = _stopwatch.ElapsedMilliseconds + "ms";
|
||||
|
||||
// 自定义log输出
|
||||
var requestInfo = JsonConvert.SerializeObject(userAccessModel);
|
||||
Parallel.For(0, 1, e =>
|
||||
uLog.opTime = _stopwatch.ElapsedMilliseconds + "ms";
|
||||
uLog.endTime = DateTime.Now;
|
||||
uLog.createTime = DateTime.Now;
|
||||
//写入文件
|
||||
if (middlewareRecordAccessLogsEnabledFileMode)
|
||||
{
|
||||
LogLockHelper.OutSql2Log("RecordAccessLogs", "RecordAccessLogs" + dt.ToString("yyyy-MM-dd-HH"), new string[] { requestInfo + "," }, false);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
// 自定义log输出
|
||||
var requestInfo = JsonConvert.SerializeObject(uLog);
|
||||
Parallel.For(0, 1, e =>
|
||||
{
|
||||
LogLockHelper.OutSql2Log("RecordAccessLogs", "RecordAccessLogs" + DateTime.Now.ToString("yyyy-MM-dd-HH"), new[] { requestInfo + "," }, false);
|
||||
});
|
||||
}
|
||||
//写入数据库
|
||||
if (middlewareRecordAccessLogsEnabledDbMode)
|
||||
{
|
||||
await _sysUserOperationLogServices.InsertAsync(uLog);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -145,18 +194,5 @@ namespace CoreCms.Net.Middlewares
|
||||
}
|
||||
}
|
||||
|
||||
public class UserAccessModel
|
||||
{
|
||||
public string User { get; set; }
|
||||
public string IP { get; set; }
|
||||
public string API { get; set; }
|
||||
public string BeginTime { get; set; }
|
||||
public string OPTime { get; set; }
|
||||
public string RequestMethod { get; set; }
|
||||
public string RequestData { get; set; }
|
||||
public string Agent { get; set; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace CoreCms.Net.Middlewares
|
||||
if (AppSettingsConstVars.MiddlewareRequestResponseLogEnabled)
|
||||
{
|
||||
// 过滤,只有接口
|
||||
if (context.Request.Path.Value.Contains("api") || context.Request.Path.Value.Contains("Api"))
|
||||
if (context.Request.Path.Value != null && (context.Request.Path.Value.Contains("api") || context.Request.Path.Value.Contains("Api")))
|
||||
{
|
||||
context.Request.EnableBuffering();
|
||||
Stream originalBody = context.Response.Body;
|
||||
@@ -57,18 +57,16 @@ namespace CoreCms.Net.Middlewares
|
||||
// 存储请求数据
|
||||
await RequestDataLog(context);
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
context.Response.Body = ms;
|
||||
await using var ms = new MemoryStream();
|
||||
context.Response.Body = ms;
|
||||
|
||||
await _next(context);
|
||||
await _next(context);
|
||||
|
||||
// 存储响应数据
|
||||
ResponseDataLog(context.Response, ms);
|
||||
// 存储响应数据
|
||||
ResponseDataLog(ms);
|
||||
|
||||
ms.Position = 0;
|
||||
await ms.CopyToAsync(originalBody);
|
||||
}
|
||||
ms.Position = 0;
|
||||
await ms.CopyToAsync(originalBody);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -91,7 +89,7 @@ namespace CoreCms.Net.Middlewares
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RequestDataLog(HttpContext context)
|
||||
private static async Task RequestDataLog(HttpContext context)
|
||||
{
|
||||
var request = context.Request;
|
||||
var sr = new StreamReader(request.Body);
|
||||
@@ -100,9 +98,9 @@ namespace CoreCms.Net.Middlewares
|
||||
|
||||
if (!string.IsNullOrEmpty(content))
|
||||
{
|
||||
Parallel.For(0, 1, e =>
|
||||
Parallel.For(0, 1, _ =>
|
||||
{
|
||||
LogLockHelper.OutSql2Log("RequestResponseLog", "RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd-HH"), new string[] { "Request Data:", content });
|
||||
LogLockHelper.OutSql2Log("RequestResponseLog", "RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd-HH"), new[] { "Request Data:", content });
|
||||
|
||||
});
|
||||
|
||||
@@ -110,18 +108,15 @@ namespace CoreCms.Net.Middlewares
|
||||
}
|
||||
}
|
||||
|
||||
private void ResponseDataLog(HttpResponse response, MemoryStream ms)
|
||||
private static void ResponseDataLog(Stream ms)
|
||||
{
|
||||
ms.Position = 0;
|
||||
var ResponseBody = new StreamReader(ms).ReadToEnd();
|
||||
// 去除 Html
|
||||
var reg = "<[^>]+>";
|
||||
var isHtml = Regex.IsMatch(ResponseBody, reg);
|
||||
if (!string.IsNullOrEmpty(ResponseBody))
|
||||
var responseBody = new StreamReader(ms).ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(responseBody))
|
||||
{
|
||||
Parallel.For(0, 1, e =>
|
||||
Parallel.For(0, 1, _ =>
|
||||
{
|
||||
LogLockHelper.OutSql2Log("RequestResponseLog", "RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd-HH"), new string[] { "Response Data:", ResponseBody });
|
||||
LogLockHelper.OutSql2Log("RequestResponseLog", "RequestResponseLog" + DateTime.Now.ToString("yyyy-MM-dd-HH"), new[] { "Response Data:", responseBody });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user