Squashed commit of the following:
commit1daa137d98Author: 不做码农 <599854767@qq.com> Date: Sat Apr 9 14:51:29 2022 +0800 IPRatelimit添加白名单接口 commitd05690654bAuthor: 不做码农 <599854767@qq.com> Date: Fri Apr 8 20:20:11 2022 +0800 添加页签openPage支持传递参数 commit861710079aAuthor: 不做码农 <599854767@qq.com> Date: Fri Apr 8 12:44:28 2022 +0800 update FileHelper.cs commita0cf47c099Author: 不做码农 <599854767@qq.com> Date: Thu Apr 7 13:30:47 2022 +0800 优化代码生成模板 commit5b376614d0Author: 不做码农 <599854767@qq.com> Date: Thu Apr 7 13:30:13 2022 +0800 IP限制增加百名单接口 commit939ec56d1dAuthor: 不做码农 <599854767@qq.com> Date: Mon Apr 4 21:48:27 2022 +0800 fix 基础sql脚本bug commit19c738b974Author: 不做码农 <599854767@qq.com> Date: Mon Apr 4 18:53:02 2022 +0800 新增加IPRateLimit限制 commit6b0e6b11b3Author: 不做码农 <599854767@qq.com> Date: Mon Apr 4 12:09:39 2022 +0800 格式化代码 commit1024471c64Author: 不做码农 <599854767@qq.com> Date: Mon Apr 4 12:02:32 2022 +0800 自定义异常新增获取LogAttribute属性
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Model;
|
||||
using IPTools.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Model.System;
|
||||
@@ -66,8 +69,15 @@ namespace ZR.Admin.WebApi.Middleware
|
||||
logLevel = NLog.LogLevel.Error;
|
||||
context.Response.StatusCode = 500;
|
||||
}
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
string responseResult = System.Text.Json.JsonSerializer.Serialize(new ApiResult(code, msg)).ToLower();
|
||||
ApiResult apiResult = new(code, msg);
|
||||
string responseResult = JsonSerializer.Serialize(apiResult, options).ToLower();
|
||||
string ip = HttpContextExtension.GetClientUserIp(context);
|
||||
var ip_info = IpTool.Search(ip);
|
||||
|
||||
@@ -83,6 +93,18 @@ namespace ZR.Admin.WebApi.Middleware
|
||||
operLocation = ip_info.Province + " " + ip_info.City,
|
||||
operTime = DateTime.Now
|
||||
};
|
||||
var endpoint = GetEndpoint(context);
|
||||
if (endpoint != null)
|
||||
{
|
||||
var logAttribute = endpoint.Metadata.GetMetadata<LogAttribute>();
|
||||
if (logAttribute != null)
|
||||
{
|
||||
sysOperLog.businessType = (int)logAttribute?.BusinessType;
|
||||
sysOperLog.title = logAttribute?.Title;
|
||||
sysOperLog.operParam = logAttribute.IsSaveRequestData ? sysOperLog.operParam : "";
|
||||
sysOperLog.jsonResult = logAttribute.IsSaveResponseData ? sysOperLog.jsonResult : "";
|
||||
}
|
||||
}
|
||||
HttpContextExtension.GetRequestValue(context, sysOperLog);
|
||||
LogEventInfo ei = new(logLevel, "GlobalExceptionMiddleware", error);
|
||||
|
||||
@@ -91,12 +113,22 @@ namespace ZR.Admin.WebApi.Middleware
|
||||
ei.Properties["status"] = 1;//走正常返回都是通过走GlobalExceptionFilter不通过
|
||||
ei.Properties["jsonResult"] = responseResult;
|
||||
ei.Properties["requestParam"] = sysOperLog.operParam;
|
||||
ei.Properties["user"] = context.User.Identity?.Name;
|
||||
ei.Properties["user"] = HttpContextExtension.GetName(context);
|
||||
|
||||
Logger.Log(ei);
|
||||
await context.Response.WriteAsync(responseResult);
|
||||
await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8);
|
||||
|
||||
SysOperLogService.InsertOperlog(sysOperLog);
|
||||
}
|
||||
|
||||
public static Endpoint? GetEndpoint(HttpContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
return context.Features.Get<IEndpointFeature>()?.Endpoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user