# 2022-04-10

### 1.4.4开源社区版:
无
### 0.3.5 专业版:
【新增】增加用户操作日志,可记录接口也可记录后台,同时支持本地存储或数据库存储。支持配置文件开启。
【新增】数据库增加用户操作日志表。
【优化】优化反射获取所有Controller 和Action的全局方法,增加缓存设置。
【优化】优化记录IP请求数据的中间件。
【修复】修复ios下用户充值余额的功能不显示的情况。
【修复】修复我的余额面板列表中右侧三角无反应的问题。
【修复】修复代理中心下线人数和订单数量统计错误的问题。#I51OUC
This commit is contained in:
JianWeie
2022-04-10 02:40:26 +08:00
parent dcefe1f4a7
commit b0360d1da4
33 changed files with 1963 additions and 277 deletions

View File

@@ -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; }
}
}